Sam Heather
Sam Heather

Reputation: 1503

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?

Upvotes: 9

Views: 11199

Answers (2)

Sam Heather
Sam Heather

Reputation: 1503

Ok - just figured it out. Changed the component beng animated from the gesture recogniser on top of the image view to the image view itself. Now, just before the code to stop the animation, I have:

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

So this is the way to do it. Thanks for the help that led me to this answer,

Sam

Upvotes: 3

Till
Till

Reputation: 27597

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;

Upvotes: 11

Related Questions