iphone - single tap gesture conflicts with double one
Asked Answered
S

1

29

I have a view. I wish to define to kinds of tap gestures for it.

So if a user single tap on the view, view will do A; and if a user double tap on the view, it will do B without doing A.

I added two UITapGestureRecognizer to the view. the single tap is with numberOfTapsRequired = 1; and the double tap is with numberOfTapsRequired = 2;

Also I set return NO for

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
     return NO;
}

However, I found that they conflict with each other. I mean, even if I double tap on the view, both A and B will be invoked.

How can I solve this problem?

Thanks

Suprasegmental answered 24/8, 2011 at 12:2 Comment(0)
D
65

You can work around this by adding the following line of code. This will make sure that the single tap recognizer only fires when the double tap recognizer failed:

    [singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
Dirkdirks answered 24/8, 2011 at 12:7 Comment(4)
but this Gives a Delay to Detect Single Tap!! :(Exit
@Mrug: True, but the only way to be 100% sure that it was a single and not a double tap is to wait until we are sure that the user isn't tapping a second time.Unfeeling
Never being that easy.Tussock
For those like @Exit who still have the delay to cancel the double tap, please refer to this solution by eladleb : https://mcmap.net/q/501553/-how-to-recognize-onetap-doubletap-at-moment and use the subclass for the double tap gesture so it sends "state failed" faster.Roar

© 2022 - 2024 — McMap. All rights reserved.