Say I am currently tracking a drag gesture. In my event handler I use a threshold to determine when the drag results in an action. When the threshold is crossed, I want to indicate that the drag gesture has completed.
The only thing I can find in the docs is this line here:
If you change this property to NO while a gesture recognizer is
currently recognizing a gesture, the gesture recognizer transitions to
a cancelled state.
So:
if (translation.y > 100) {
// do action
[self doAction];
//end recognizer
sender.enabled = NO;
sender.enabled = YES;
}
This works but it looks like there might be a neater way.
Does anyone know of another way to indicate that a gesture has ended programmatically? I would expect something like a method -end: that generates a final event with state UIGestureRecognizerStateEnded.
Have you defined a custom UIGestureRecognizer? If the gesture you’re recognizing is different from the standard ones defined by Apple because it has a different threshold or is otherwise not the same as a regular UIPanGestureRecognizer, then it might make sense to create your own UIGestureRecognizer. (see subclassing notes)
If you have subclassed UIGestureRecognizer, you can simply set the state like this:
You probably want to do this in the touchesMoved:withEvent: method. Also note:
“Subclasses of UIGestureRecognizer must import UIGestureRecognizerSubclass.h. This header file contains a redeclaration of state that makes it read-write.”
On the other hand, if you’re only implementing a UIGestureRecognizerDelegate, the state is read-only, and there is no way to directly set it. In that case your method of disabling/enabling might be the best you can do.