How to create buttons that are activated when the user slides a finger on them on the iPhone

I am writing an iPhone application that has a piano-like interface. The user has several large buttons with no spaces between them. At the moment, I created IBActions in Interface Builder with the right mouse button, dragging the buttons into the corresponding controller interface file. This creates a method:

-(IBAction) buttonTouchDown: (id) sender

In the body of this function, I have included code that responds to this action.

This works when I press a button, but when I drag my finger over several buttons, only the first is activated. When I drag my finger over the buttons, I need the first one to activate while my finger is above it. Then, when my finger leaves the button and enters the second button, I need the second to activate, and the first to deactivate.

Any advice would be greatly appreciated!

+3
source share
3 answers

You can also use the touchhesBegan function. With this function, you can easily tell what will happen if a button (or several buttons) is pressed / touched.

The code should look like this:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [[event touchesForView:self.view] anyObject];

    if(touch == YourButton){
         //so some stuff after touching the button

    }
}

Remember to leave the button in your .h as IBOutlet UIButton * MyButton;

+1
source

I have not tried this, but I will try to add the newly added Grecure Recognizer to your button. Select the direction of the slide (Left / Right) and implement the [button setEnabled] function accordingly. Something like, when a swipe occurs, you set the button to turn on, and all the other buttons are disabled.

Peace

0
source

- . UIViews , . .

0

All Articles