我有一个UIPanGestureRecognizer附加到我的iOS应用程序中的视图.我从平底锅处理程序的Touches示例应用程序中复制了代码.手势开始时,我的代码:
将锚点和中心更改为围绕用户的手指,如下所示:
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;
Run Code Online (Sandbox Code Playgroud)随着手势的进行,平移处理器不断地改变中心以跟踪手指的移动.到现在为止还挺好.
当用户放手时,我希望视图动画回到其原始起点.执行此操作的代码如下所示:
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationCurveEaseOut animations:^{
target.center = originalCenter;
target.layer.anchorPoint = originalAnchorPoint;
}];
Run Code Online (Sandbox Code Playgroud)
这确实使视图回归到其原始起点.但是,在动画开始之前,视图会跳转到UI中的其他位置.IOW,放手,它跳跃,然后它动画回到它所属的位置.
我想也许我需要设置锚点并在动画外部居中,也许将中心设置为超级视图中的位置,就像手势开始时一样,但这似乎没有区别.
我在这里错过了什么?当用户放手时,如何防止跳转?