Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detecting finger up/down UITapGestureRecognizer

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:)]
like image 329
Dani Avatar asked Dec 06 '11 01:12

Dani


2 Answers

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.

like image 78
Stuart Avatar answered Sep 30 '22 08:09

Stuart


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

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

    return YES;
}
like image 27
Toydor Avatar answered Sep 30 '22 07:09

Toydor