detecting finger up/down UITapGestureRecognizer

Dani picture Dani · Dec 6, 2011 · Viewed 8.1k times · Source

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:)]

Answer

Stuart picture Stuart · Dec 6, 2011

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.