detecting finger up/down UITapGestureRecognizer

坚强是说给别人听的谎言 提交于 2019-12-06 20:50:05

问题


How can I know when the finger is down and when is it up with UITapGestureRecognizer?
The documentation says I should only handle UIGestureRecognizerStateEnded as tap so it means there is UIGestureRecognizerStateBegin when finger is down, but all I get is UIGestureRecognizerStateEnded.
The code I use to register the recognizer is:

[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]

回答1:


UITapGestureRecognizer is a discrete gesture recognizer, and therefore never transitions to the began or changed states. From the UIGestureRecognizer Class Reference:

Discrete gestures transition from Possible to either Recognized (UIGestureRecognizerStateRecognized) or Failed (UIGestureRecognizerStateFailed), depending on whether they successfully interpret the gesture or not. If the gesture recognizer transitions to Recognized, it sends its action message to its target.

(Remembering of course that UIGestureRecognizerStateRecognized == UIGestureRecognizerStateEnded).

The docs are saying that you should check the state of a tap gesture recognizer to see that it is in its ended state, before you fire your code to say that it has been recognized. They are not saying that the tap gesture actually transitions to the began or changed states (although I admit that the docs are a little misleading in the language used!).

If you want to check for the finger down event for a tap gesture recognizer, I would recommend just using touchesBegan:withEvent:, since this is what you are really after anyway.




回答2:


You could override the delegate method -(BOOL)gestureRecognizer:shouldReceiveTouch:.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    NSLog(@"Hello from press down");

    return YES;
}


来源:https://stackoverflow.com/questions/8394256/detecting-finger-up-down-uitapgesturerecognizer

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