I have a UIPanGestureRecognizer attached to a view in my iOS app. I copied the code from the Touches sample app for the pan handler. When the gesture starts, my code:
- Records the original anchor point and center.
Changes the anchor point and center to be around the user's fingers, like so:
CGPoint locationInView = [gestureRecognizer locationInView:target]; CGPoint locationInSuperview = [gestureRecognizer locationInView:target.superview]; target.layer.anchorPoint = CGPointMake( locationInView.x / target.bounds.size.width, locationInView.y / target.bounds.size.height ); target.center = locationInSuperview;
As the gesture progresses, the pan handler continuously changes the center to track the finger movement. So far so good.
When the user lets go, I want the view to animate back to its original starting point. The code that does that looks like this:
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationCurveEaseOut animations:^{
target.center = originalCenter;
target.layer.anchorPoint = originalAnchorPoint;
}];
And that does animate the view back to its original starting point. However, before the animation starts, the view jumps to a different spot in the UI. IOW, let go, it jumps, then it animates back to where it belongs.
I thought perhaps I needed to set the anchor point and center outside the animation, perhaps setting the center to the location in the super view, like when the gesture begins, but that seems to make no difference.
What am I missing here? How can I prevent the jump when the user lets go?