Search code examples
ioscocoa-touchuiviewcore-animation

Cancelling UIView Animation - self.view.layer removeAllAnimations not working


I've got a UIView Animation going on that I need to cancel in my iOS app. I've tried this:

[self.view.layer removeAllAnimations];

But it didn't work. The animation continued. Here is my animation code:

[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");
        }];

Does anybody have any ideas as to why it's not working?


Solution

  • You are adding that animation to recognizer's view, hence you will have to remove it from that same view's layer.

    So instead of

    [self.view.layer removeAllAnimations];
    

    you may want to

    [recognizer.view.layer removeAllAnimations];
    

    And to keep the current status of the transformation, fetch that one from the presentation layer. The presentation layer is the one that actually reflects the changes during the animation.

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