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

Jackson Tale
  • 25,428
  • 34
  • 149
  • 271

1 Answers1

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];
Markus Müller-Simhofer
  • 3,391
  • 1
  • 28
  • 32
  • 3
    but this Gives a Delay to Detect Single Tap!! :( – Mrug Mar 07 '14 at 08:00
  • 4
    @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. – Markus Müller-Simhofer Mar 14 '14 at 10:00
  • Never being that easy. – tounaobun Aug 04 '15 at 07:06
  • 1
    For those like @Mrug who still have the delay to cancel the double tap, please refer to this solution by eladleb : http://stackoverflow.com/a/23415324/1158074 and use the subclass for the double tap gesture so it sends "state failed" faster. – Michael Pirotte Dec 04 '15 at 13:38