I’m having problems simply translating a view with UIPanGestureRecognizer while setting the UILabel’s text to the gestures velocity.
I have been successfull translating a view with a pan gesture like this (with the help of IB):
- (IBAction)handleGesture:(UIPanGestureRecognizer *)sender
{
CGPoint translation = [sender translationInView:self.view];
sender.view.center = CGPointMake(sender.view.center.x + translation.x, sender.view.center.y + translation.y);
[sender setTranslation:CGPointMake(0, 0) inView:self.view];
}
But when I add the following line to this method the view stops translating but the UILabel gets updated with the velocity:
self.myLabel.text = [NSString stringWithFormat:@"%@", [NSNumber numberWithInt:[sender velocityInView:self.roundShape].y]];
How can I fix this?
Basically, the translation value you get from the Pan Gesture Recognizer is a relative value. Relative to the starting point when the gesture began recognizing the touch. So you want to do is hold onto the original point the gesture started at and apply the translation to that point, not to the view’s current location.
I can’t think of a time where you would want to zero-out the translation value to a recognizer while it is recognizing.