停止 animateWithDuration

Stop animateWithDuration

我在 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];
        }
    }];
}

我想停止这个动画 当 viewController viewWillDisappear:

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

因为动画与dismissViewcontrollerAnimation冲突。

问题:

[_backgroundView.layer removeAllAnimations];

不工作...

如何停止动画?

帮帮我,谢谢。

您正在正确取消动画:

[_backgroundView.layer removeAllAnimations];

但是您可能会忘记导入 QuartzCore.h:

#import <QuartzCore/QuartzCore.h>

如果没有帮助,试试这个:

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

如果没有帮助,请尝试将此行添加到上面的代码中:

[CATransaction flush];

@KlimczakM 的解决方案对我不起作用。

我是 运行 一个可以移动和成像的 'animateWithDuration' 块,我使用下一个代码来暂停和恢复动画:

-(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 link 的 Apple 文档。