the*_*ory 2 animation cocoa-touch uiview cglayer
我有一个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,放手,它跳跃,然后它动画回到它所属的位置.
我想也许我需要设置锚点并在动画外部居中,也许将中心设置为超级视图中的位置,就像手势开始时一样,但这似乎没有区别.
我在这里错过了什么?当用户放手时,如何防止跳转?
Kru*_*lur 11
我怀疑有两个问题没有尝试你在做什么:
更改定位点会更改视图/图层的位置.为了在不修改位置的情况下更改锚点,您可以使用以下一些帮助:
-(void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view
{
CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y);
CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y);
newPoint = CGPointApplyAffineTransform(newPoint, view.transform);
oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform);
CGPoint position = view.layer.position;
position.x -= oldPoint.x;
position.x += newPoint.x;
position.y -= oldPoint.y;
position.y += newPoint.y;
view.layer.position = position;
view.layer.anchorPoint = anchorPoint;
}
Run Code Online (Sandbox Code Playgroud)
(我在我的项目中自己使用它.发现在这里:更改我的CALayer的anchorPoint移动视图)
您的动画将锚点设置回原始值. 您应该使用上面的帮助程序来重置锚点.这可确保在更改锚点时视图不会移动.你必须在动画之外做到这一点.然后,使用动画块更改视图的中心并将其设置为您想要的位置.
| 归档时间: |
|
| 查看次数: |
2125 次 |
| 最近记录: |