Double tap on a button

2019-02-15 08:01发布

问题:

How can I add an action for a double tap on my button?

回答1:

- (void) buttonTouchDownRepeat:(id)sender event:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    if(touch.tapCount == 2) {
        NSLog(@"Twice");
    }
    else {
        NSLog(@"otherwise");
    }
}


回答2:

In IB or code, connect an action to the button's UIControlEventTouchDownRepeat event. The action method should have a signature like this:

- (void) buttonTouchDownRepeat:(id)sender event:(UIEvent *)event

In the method's implementation, you can access a UITouch instance with [[event allTouches] anyObject] and then check the touch's tapCount value.



回答3:

Of course, if you want to be super StackOverFlow cool programming wiz? Then use UITapGestureRecognizer...

Granted it's only available for recent iOS, don't try it on 3.0;)