The velocityInView
method is defined only when a pan occurs. That is, only when you're actually moving the finger a pan gesture is occurring. If you keep your finger still, it does not actually trigger a pan gesture.
This means that there is no built-in method to know the movement speed when the gesture ends. You could do something like check the time difference between the last event with the status value as UIGestureRecognizerStateChanged
and UIGestureRecognizerStateEnded
. You can then tune this threshold in order to obtain the desired behavior.
For example
- (IBAction) panGestureRecognized:(UIPanGestureRecognizer *)recognizer {
UIGestureRecognizerState state = recognizer.state;
CGPoint gestureTranslation = [recognizer translationInView:self];
CGPoint gestureVelocity = [recognizer velocityInView:self];
if ( state == UIGestureRecognizerStateChanged )
_lastChange = CFAbsoluteTimeGetCurrent();
else if ( state == UIGestureRecognizerStateEnded ) {
double curTime = CFAbsoluteTimeGetCurrent();
double timeElapsed = curTime - _lastChange;
if ( timeElapsed < MY_THRESHOLD )
finalSpeed = gestureVelocity;
else
finalSpeed = CGPointZero;
}
}