Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 844
Sam Heather Avatar asked Sep 15 '12 18:09

Sam Heather


2 Answers

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;
like image 131
Till Avatar answered Oct 17 '22 04:10

Till


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

like image 26
Sam Heather Avatar answered Oct 17 '22 05:10

Sam Heather