为什么设置我的看法跳的时候层anchorPoint在动画块? [英] Why does my view jump when setting the layer anchorPoint in an animation block?

查看:177
本文介绍了为什么设置我的看法跳的时候层anchorPoint在动画块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经连接到我的iOS应用景色的UIPanGestureRecognizer。我复制从<一的code href=\"https://developer.apple.com/library/ios/sample$c$c/Touches/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007435\"相对=nofollow>倒是对盘支架示例应用程序。当手势开始时,我的code:

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.

当用户让去,我要以动画回到原来的起点。在code,做看起来是这样的:

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;
}];

和,做动画视图回到原来的起点。然而,动画开始前,视图跳转到用户界面不同的地方。 IOW,放手,它跳跃的话,就以动画方式返回原处。

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.

我缺少的是在这里吗?我怎样才能prevent当用户让去跳?

What am I missing here? How can I prevent the jump when the user lets go?

推荐答案

我怀疑不尝试你在做什么两个问题:

I suspect two issues without trying out what you are doing:

更改锚点改变视图的/层的位置为了改变锚点没有位置的修改,你可以使用一些帮助像这样的:

Changing the anchor point changes a view's/layer's position. In order to change an anchor point without modification of the position, you can use some helper like this one:

-(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;
}

(我用我自己在我的项目在这里找到:<一href=\"http://stackoverflow.com/questions/1968017/changing-my-calayers-anchorpoint-moves-the-view\">Changing我的CALayer的anchorPoint移动视图)

您的动画设置锚点​​回到其原始值。
你应该使用上面的助手重置锚点。这确保了改变锚当视图不会移动。你必须做动画这个之外。之后,使用动画块来改变视图的中心,它的动画到你希望它是。

Your animation sets the anchor point back to its original value. You should use the helper above to reset the anchor point. This ensures that the view won't move when changing the anchor. You'll have to do this outside of the animation. Afterwards, use an animation block to change the view's center and animate it to where you want it to be.

这篇关于为什么设置我的看法跳的时候层anchorPoint在动画块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆