When using UIPanGestureRecognizer
and detecting UIGestureRecognizerStateEnded
, then the velocity of the gesture is not the true velocity. Instead, it's the old velocity of the previous invocation of my action method. How can I access the true velocity at the end of the gesture?
I create my UIPanGestureRecognizer
like this:
UIPanGestureRecognizer* panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognized:)];
[panGestureRecognizer setMaximumNumberOfTouches:2];
[panGestureRecognizer setMinimumNumberOfTouches:1];
[panGestureRecognizer setDelegate:self];
[panGestureRecognizer setDelaysTouchesBegan:NO];
[panGestureRecognizer setDelaysTouchesEnded:NO];
[panGestureRecognizer setCancelsTouchesInView:NO];
[self addGestureRecognizer:panGestureRecognizer];
The beginning of my action method is here:
- (IBAction) panGestureRecognized:(UIPanGestureRecognizer *)recognizer {
UIGestureRecognizerState state = recognizer.state;
CGPoint gestureTranslation = [recognizer translationInView:self];
CGPoint gestureVelocity = [recognizer velocityInView:self];
[CBAppDelegate log:@"panGestureRecognized: state: %s\n translation: (%f, %f)\n velocity: (%f, %f)", [self toString:state], gestureTranslation.x, gestureTranslation.y, gestureVelocity.x, gestureVelocity.y];
Example of the log output:
2013-09-30_10:46:32.830 panGestureRecognized: state: UIGestureRecognizerStateChanged
translation: (-283.000000, 2.000000)
velocity: (-43.046783, 45.551472)
2013-09-30_10:47:02.942 panGestureRecognized: state: UIGestureRecognizerStateEnded
translation: (-283.000000, 2.000000)
velocity: (-43.046783, 45.551472)
As you can see, the velocity is the same in both log entries (same story with translation, but I care only about velocity), although I was holding down my finger for about 30 seconds without moving it, and then lifting the finger. You can tell the timing from the timestampts of the entries. There should certainly not be a velocity reported after 30 seconds of not moving my finger.
I've tested this with the iOS simulator for iPhone with iOS 6.1.