xx11dragon
xx11dragon

Reputation: 166

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.

Upvotes: 3

Views: 4149

Answers (2)

ChavirA
ChavirA

Reputation: 707

Te solution from @KlimczakM didn't work for me.

I'm running an 'animateWithDuration' block that moves and image, and I use the next code to pause and resume the animation:

-(void)pauseLayer:(CALayer*)layer {
   CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
   layer.speed = 0.0;
   layer.timeOffset = pausedTime;
}

-(void)resumeLayer:(CALayer*)layer {
   CFTimeInterval pausedTime = [layer timeOffset];
   layer.speed = 1.0;
   layer.timeOffset = 0.0;
   layer.beginTime = 0.0;
   CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
   layer.beginTime = timeSincePause;
}

This is from the Apple Documentation at this link.

Upvotes: 0

KlimczakM
KlimczakM

Reputation: 12914

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];

Upvotes: 2

Related Questions