filtering single and double taps

那年仲夏 提交于 2019-11-28 07:33:25

Check this, seems right what you are looking for:

Below the code to use UITapGestureRecognizer to handle single tap and double tap. The code is designed that it won't fire single tap event if we got double tap event. The trick is use requireGestureRecognizerToFail to ask the single tap gesture wait for double tap event failed before it fire. So when the user tap on the screen, the single tap gesture recognizer will not fire the event until the double tap gesture recognizer. If the double tap gesture recognizer recognize the event, it will fire a double tab event and skip the single tap event, otherwise it will fire a single tap event.

    UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
                            initWithTarget:self action:@selector(handleDoubleTap:)];
    doubleTapGestureRecognizer.numberOfTapsRequired = 2;

    //tapGestureRecognizer.delegate = self;
    [self addGestureRecognizer:doubleTapGestureRecognizer];

    //
    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    singleTapGestureRecognizer.numberOfTapsRequired = 1;

    [singleTapGestureRecognizer requireGestureRecognizerToFail: doubleTapGestureRecognizer];
    //tapGestureRecognizer.delegate = self;
    [self addGestureRecognizer:singleTapGestureRecognizer];

Possibly I don't understand exactly what you mean by "i need to keep track of the points", but I don't see any problems with doing this with a gesture recognizer.

Surya Kameswara Rao Ravi

Swift 3 Solution:

doubleTap = UITapGestureRecognizer(target: self, action:#selector(self.doubleTapAction(_:)))
doubleTap.numberOfTapsRequired = 2


singleTap = UITapGestureRecognizer(target: self, action:#selector(self.singleTapAction(_:)))
singleTap.numberOfTapsRequired = 1

singleTap.require(toFail: doubleTap)

self.view.addGestureRecognizer(doubletap)
self.view.addGestureRecognizer(singleTap)

In the code line singleTap.require(toFail: doubleTap) we are forcing the single tap to wait and ensure that the tap even is not a double tap. Which means we are asking to ensure the double tap event has failed hence it is concluded as a single tap.

Swift Solution :

    doubleTapSmall1.numberOfTapsRequired = 2
    doubleTapSmall1.addTarget(self, action: "doubleTapPic1")
    smallProfilePic1.addGestureRecognizer(doubleTapSmall1)

    singleTapSmall1.numberOfTapsRequired = 1
    singleTapSmall1.addTarget(self, action: "singleTapPic1")
    singleTapSmall1.requireGestureRecognizerToFail(doubleTapSmall1)
    smallProfilePic1.addGestureRecognizer(singleTapSmall1)

Just implement the UIGestureRecognizer delegate methods by setting the delegate properly.

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
            return  YES;
        }

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

Also add this line of code

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