How to create buttons which are activated when the

2019-04-11 17:49发布

I'm writing an iPhone application which has a piano like interface. The user is presented with a number of large buttons with no spaces between them. At the moment I've created IBActions in Interface Builder by right mouse dragging the buttons into the appropriate view controller interface file. This creates a method:

-(IBAction) buttonTouchDown: (id) sender

In the body of this function I've included the code which responds to this action.

This works when I tap the button but but when I drag my finger over a number of buttons only the first one activates. When I drag my finger over the buttons I need the first one to activate while my finger is over 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条回答
欢心
2楼-- · 2019-04-11 18:17

You can also use the function touchesBegan. With this function you can easily say what would happen if a button (or multiple buttons) are being pressed/touched.

Code should look like:

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

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

    }
}

Don't forget to decare the button in your .h like IBOutlet UIButton * MyButton;

查看更多
趁早两清
3楼-- · 2019-04-11 18:20

Buttons are not the way to go. UIViews have various touch methods that would work better. Or, use a custom gesture recognizer.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-04-11 18:36

I haven't tried this up but try adding the newly added Gesture Recognizer to your button. Select the slide direction (Right/Left) and implement the [button setEnabled] function accordingly. Something like when the swipe happens, you set the button to be enabled and all other buttons to disable.

Peace

查看更多
登录 后发表回答