取消UIView动画 - self.view.layer removeAllAnimations无效

时间:2012-09-15 18:46:02

标签: ios cocoa-touch uiview core-animation

我有一个UIView动画,我需要在我的iOS应用程序中取消。我试过这个:

[self.view.layer removeAllAnimations];

但它没有用。动画继续。这是我的动画代码:

[UIView animateWithDuration:1.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
recognizer.view.transform = CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y);
} completion:^(BOOL finished) {

            NSLog(@"completed animation, now do whatever");
        }];

有没有人知道为什么它不起作用?

2 个答案:

答案 0 :(得分:10)

您正在将该动画添加到识别器的视图中,因此您必须将其从同一视图的图层中删除。

所以而不是

[self.view.layer removeAllAnimations];
你可能想要

[recognizer.view.layer removeAllAnimations];

要保持转换的当前状态,请从表示层获取该转换。表示层是实际反映动画期间变化的表示层。

recognizer.view.layer.transform = recognizer.view.layer.presentationLayer.transform;

答案 1 :(得分:3)

好的 - 刚想通了。将从图像视图顶部的手势识别器动画的组件更改为图像视图本身。现在,就在停止动画的代码之前,我有:

 truckView.frame = [[trackView.layer presentationLayer] frame];
 [truckView.layer removeAllAnimations];

所以这是做到这一点的方法。感谢帮助我得到这个答案,

萨姆