0

我目前正在为对象设置动画,position使其以 50 的间隔在 x 轴上移动。但是,我希望能够在某些情况下停止对象并更改其动画,例如用户在其前面放置对象。

我必须使用[self.layer setPosition:newPosition],否则动画完成后对象将弹回其原始位置。如果对象被放下以停止它,则对象会setPosition在提交新动画之前反弹到该点。如何让它停止并停留在原处并执行新动画?

4

1 回答 1

1

You are using implicit layer animation to animate the positions of your layers. To cancel an animation before it is complete, you would use:

[self.layer removeAllAnimations];

This would have the side effect of jumping your layer to the final animation position. If you want the layer to stop where it is, then you need to get the current presentationLayer position.

CGPoint currentAnimationPosition = self.presentationLayer.position;
self.layer.position = currentAnimationPosition;
[self.layer removeAllAnimations];

To perform a new animation, you can simply set a new position on the layer as you were doing before.

于 2013-07-04T07:01:53.633 回答