3
votes

I am adding two gesture recognizers to a view with the code below. What I want is: if the user taps once one thing happens. If they tap twice another thing happens.

As it is, the single tap action occurs whether the user taps once or twice. In other words when I double tap I get two actions, instead of one as I would prefer

differentiate

//gesture recognizer.
UITapGestureRecognizer *tapRecognizerShowHideMenu = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doOneThing)];
[tapRecognizerShowHideMenu setDelegate:self];
[tapRecognizerShowHideMenu setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tapRecognizerShowHideMenu];

UITapGestureRecognizer *tapRecognizerEditEffect = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doAnotherThing)];
[tapRecognizerEditEffect setDelegate:self];
[tapRecognizerEditEffect setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:tapRecognizerEditEffect];
4

4 Answers

16
votes

Add this to your code:

[tapRecognizerShowHideMenu requireGestureRecognizerToFail:tapRecognizerEditEffect];

When you set requireGestureRecognizerToFail: for a gesture recognizer, you're saying that it should only recognize the gesture if the other gesture recognizer did not. So here, you tell the single tap to be recognized only when the double tap was not.

1
votes

I think the following link could be helpful to you, you can do it by either Overriding Touch Event Handlers, or use Gesture Recognizers.

http://www.cimgf.com/2010/06/14/differentiating-tap-counts-on-ios/

1
votes

This seems to work pretty well, modified from the link given by Anton in a comment above.

How to recognize oneTap/doubleTap at moment?

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if (touch.tapCount == 1) {

        [self performSelector:@selector(showHideMenu) withObject:nil afterDelay:0.15];

    } else if (touch.tapCount == 2) {

        [self performSelector:@selector(editEffect) withObject:nil afterDelay:0];
    }
}
0
votes

Maybe it will be more useful to use long tap gesture recognizer instead of double tap gesture recognizer. And then you won't have such conflicts