【发布时间】:2012-05-15 03:26:36
【问题描述】:
我有一个 UIPanGestureRecognizer 附加到我的 iOS 应用程序中的视图。我从Touches sample app 复制了用于平移处理程序的代码。当手势开始时,我的代码:
- 记录原始锚点和中心。
-
将锚点和中心更改为围绕用户的手指,如下所示:
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;
随着手势的进行,平移处理程序不断改变中心以跟踪手指移动。到目前为止一切顺利。
当用户放手时,我希望视图动画回到其原始起点。执行此操作的代码如下所示:
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationCurveEaseOut animations:^{
target.center = originalCenter;
target.layer.anchorPoint = originalAnchorPoint;
}];
这确实将视图动画化回其原始起点。但是,在动画开始之前,视图会跳转到 UI 中的不同位置。 IOW,放手,它跳起来,然后动画回到它所属的地方。
我想也许我需要在动画之外设置锚点和中心,也许将中心设置为超级视图中的位置,比如手势开始时,但这似乎没有区别。
我在这里缺少什么?用户放手时如何防止跳转?
【问题讨论】:
-
确保你了解什么是锚点:stackoverflow.com/a/22188420/550393
标签: cocoa-touch animation uiview cglayer