Stop an auto-reverse / infinite-repeat UIView animation with a BOOL / completion block
Asked Answered
A

5

29

I'm setting up the following UIView animateWithDuration: method, with the intention of setting my animationOn BOOL elsewhere in the program to cancel that infinite looped repeat. I was under the impression that the completion block would be called each time a cycle of the animation ends, but this doesn't appear to be the case.

Is the completion block ever called in a repeating animation? And if not, is there another way I can stop this animation from outside this method?

- (void) animateFirst: (UIButton *) button
{
    button.transform = CGAffineTransformMakeScale(1.1, 1.1);
    [UIView animateWithDuration: 0.4
                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
                     animations: ^{
                         button.transform = CGAffineTransformIdentity;
                     } completion: ^(BOOL finished){
                         if (!animationOn) {
                             [UIView setAnimationRepeatCount: 0];
                         }
    }];
}
Allamerican answered 21/12, 2012 at 13:55 Comment(0)
P
56

The completion block will only get called when the animation is interrupted. For example it gets called when the app goes in the background and comes back to the foreground again (via multitasking). In that case the animation is stopped. You should restart the animation when that happens.

To stop the animation you can remove it from the view's layer:

[button.layer removeAllAnimations];
Phenacetin answered 21/12, 2012 at 13:59 Comment(5)
That didn't work, I'm afraid. Animation is still going after the change in transform is applied. (I set the .transform to CGAffineTransformMakeScale(1.0, 1.0)).Allamerican
I found something else that might work. I updated my answer. Can you try that?Phenacetin
Oh you figured it out yourself :) Thanks for accepting my answer anywayPhenacetin
Yours appeared pretty much as I hit Add on mine, so I figured that meant you were typing it first ;)Allamerican
@TomvanZummeren But this will remove every single animation.... what if you have multiple animation blocks and you only want to stop one of the animation blocks....Bounder
S
9

Old but another option.

You can also setup another animation which is not repeating on the same view, that way you can also capture it in the current state and return it to how it is by using the option UIViewAnimationOptionBeginFromCurrentState. Your completion block is also called.

-(void)someEventSoStop
{
    button.transform = CGAffineTransformMakeScale(1.0, 1.0);
    [UIView animateWithDuration: 0.4
                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
                     animations: ^{
                         button.transform = CGAffineTransformIdentity;
                     } completion: ^(BOOL finished){

                     }];
}
Stopgap answered 26/2, 2015 at 12:7 Comment(1)
This is a nicer approach than the other solution as it smoothly brings the animation back to identity.Resiniferous
A
2

I've solved the problem by calling [button.layer removeAllAnimations].

Allamerican answered 21/12, 2012 at 14:11 Comment(1)
I tired this but it doesn't work for me. Where in your code do you call this method from?Bounder
G
1

As per the documentation of View class reference: If you used any of the class methods such as animateWithDuration:delay:options:animations:completion: if the duration is set to negative value or 0, the changes are made without performing animation. so I did something like this to stop the infinite animation:

[UIView animateWithDuration:0.0 animations:^{
      button.layer.affineTransform = CGAffineTransformIdentity;
  }];

I think this is better than removing all animations from the layer as in the suggested answer. Note that this is applicable for all other class animation methods in the UIView class.

Glyceric answered 21/1, 2016 at 19:28 Comment(1)
This did not work for me. I had to call removeAllAnimations.Neutrophil
R
0

Calling removeAllAnimations() works. However, calling this method stops the animations and apply the transformations inside the UIView.animate block outright, thus causing an unsmooth transition. To be able to achieve a smooth transition when you remove animations, you can use UIView.transition which will smoothly transition the current view state to the final state using a cross-dissolve animation, hence achieving a smooth transition:

UIView.transition(with: stackView, duration: 0.3, options: .transitionCrossDissolve) {
    self.button.layer.removeAllAnimations()
    self.button.alpha = 1
}
Rodl answered 11/7, 2023 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.