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

Sam*_*her 9 cocoa-touch core-animation uiview ios

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

[self.view.layer removeAllAnimations];
Run Code Online (Sandbox Code Playgroud)

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

[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");
        }];
Run Code Online (Sandbox Code Playgroud)

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

Til*_*ill 10

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

而不是

[self.view.layer removeAllAnimations];
Run Code Online (Sandbox Code Playgroud)

你可能想要

[recognizer.view.layer removeAllAnimations];
Run Code Online (Sandbox Code Playgroud)

并保持转换的当前状态,从表示层获取该转换.表示层是实际反映动画期间更改的层.

recognizer.view.layer.transform = recognizer.view.layer.presentationLayer.transform;
Run Code Online (Sandbox Code Playgroud)