Search code examples
iosuianimation

Stop animateWithDuration


I have a cycle animate in viewController

- (void)moveAnimating
{
    [UIView animateWithDuration:2.0f animations:^{
        _backgroundView.center = CGPointMake(self.center.x , self.center.y - kMoveDistanceHeight);
     } completion:^(BOOL finished) {
        if (_backgroundView.animating)
        {
           [_backgroundView moveAnimating];
        }
    }];
}

I want stop this animate When the viewController viewWillDisappear:

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    _backgroundView.animating = NO;
    [_backgroundView.layer removeAllAnimations];
}

Because the animation is conflict with dismissViewcontrollerAnimation.

Question:

[_backgroundView.layer removeAllAnimations];

not work...

How to stop the animation?

Help me,thanks.


Solution

  • You are canceling animations correctly:

    [_backgroundView.layer removeAllAnimations];
    

    But you might forget about importing QuartzCore.h:

    #import <QuartzCore/QuartzCore.h>
    

    If it doesn't help try this:

    [CATransaction begin];
    [_backgroundView.layer removeAllAnimations];
    [CATransaction commit];
    

    If it doesn't help try to add this line to the code above:

    [CATransaction flush];