iOS - Multiple Tap gesture recognizers

江枫思渺然 提交于 2019-11-30 04:53:28

问题


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?


回答1:


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.




回答2:


Change your 2 requireGestureRecognizerToFail calls to:

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


来源:https://stackoverflow.com/questions/13866623/ios-multiple-tap-gesture-recognizers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!