iOS - Multiple Tap gesture recognizers
Asked Answered
M

2

6

In my application, i've to detect single, double and triple taps. So, I'm using UITapGestureRecognizer. I'm using the following code:

    UITapGestureRecognizer *oneTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGestureOnAnimal:)];
    oneTap.numberOfTapsRequired = 1;

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTapGestureOnAnimal:)];
    doubleTap.numberOfTapsRequired = 2;
    [doubleTap requireGestureRecognizerToFail:oneTap];

    UITapGestureRecognizer* tripleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTripleTapGestureOnAnimal:)];
    tripleTap.numberOfTapsRequired = 3;
    [tripleTap requireGestureRecognizerToFail:doubleTap];

    [self addGestureRecognizer:oneTap];
    [self addGestureRecognizer:doubleTap];
    [self addGestureRecognizer:tripleTap];

But the problem is that its always detect single and double taps only. Its not detecting triple tap at all.... can some one point out the mistake that I am doing to detect triple taps?

Monteverdi answered 13/12, 2012 at 19:2 Comment(0)
E
17

Check with this,

[oneTap requireGestureRecognizerToFail:doubleTap];
[oneTap requireGestureRecognizerToFail:tripleTap];
[doubleTap requireGestureRecognizerToFail:tripleTap];

You had switched the taps in the methods and you were not doing the second line above. Ideally one tap should be detected only when double tap and triple tap fails. And double tap should be detected when triple tap fails.

Euryale answered 13/12, 2012 at 19:9 Comment(7)
Still its not detecting triple tapMonteverdi
@Satyamsvv, If you comment out the other two gestureRecognizers, is it working?Euryale
@Satyamsvv, Any updates? Was it detecting when the other two are not added to self? This should work. If it is not working, the issue is related to something else.Euryale
No, its still not working. That's the only code written in that class which is a subview of UIImageView.Monteverdi
@Satyamsvv, That's weird. If that is the only gesture, then also it is not detecting? Is there anything else different from the other two gestures? You have removed the other lines before putting the above right? Check this #9244368. They are adding 3 tap gestures.Euryale
Its working perfect..... dont know what's the issue, just reset the simulator and ran the app again.... it started working.Monteverdi
@iDev, is it possible to do this with delegate?Villosity
L
0

Change your 2 requireGestureRecognizerToFail calls to:

[oneTap requireGestureRecognizerToFail:tripleTap];
[oneTap requireGestureRecognizerToFail:doubleTap];
[doubleTap requireGestureRecognizerToFail:tripleTap];  
Latinalatinate answered 13/12, 2012 at 19:11 Comment(3)
Still its not detecting triple tapMonteverdi
I created a test project and it worked. Maybe there is something else in your view that is messing with the gestures. Try creating a project with only one view and add the three gestures and you'll see that it's correct. What else is in your view? I can try to help.Latinalatinate
I created a class inherited from UIImageView and handling the taps in that class (as mentioned in the description).Monteverdi

© 2022 - 2024 — McMap. All rights reserved.