ios – 停止-removeAllAnimaitions的调用

目前在iOS平台上,当按下主页按钮时,-removeAllAnimations将发送到每个视图的图层属性.

我迫切需要阻止这种行为!

我目前正在使用CABasicAnimation来动画绘制圆形以充当计时器.

这是一个过程的GIF.计时器启动,动画开始顺利(原谅可怜的fps).按下主页按钮后,我回到应用程序,动画立即结束.

《ios – 停止-removeAllAnimaitions的调用》

我正在使用 – (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)标志方法来检测定时器何时停止.这一切都很顺利,直到我按下主页按钮.

按home键立即完成动画并过早完成计时器.

有没有办法阻止这种行为,比如UIViewController上的方法或我可以在项目的.plist中设置的某些属性?

这是我的第一个陈述的来源. CABasicAnimation disappear when home button is pushed

以下是我目前解决问题的尝试.

>子类CALayer覆盖-removeAllAnimations以防止动画停止.
>执行方法在CALayer类的扩展中的load方法中调整swizzles -removeAllAnimations到某些伪造方法.我已经确定正在调用伪方法,但仍然会遇到相同的行为.

最佳答案 我创建了一个示例,使用CABasicAnimation为CALayer的位置设置动画.

这个想法是在应用程序转到后台时存储动画的一些上下文,并在应用程序返回时恢复动画.

@interface ViewController ()
{
    BOOL animationStoped ;
    CFTimeInterval time ;
    CGPoint position ;
}

@property (nonatomic, strong) UIView *viewAnimate ;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _viewAnimate = [[UIView alloc] initWithFrame:CGRectMake(0.0, 100.0, 100.0, 100.0)] ;
    _viewAnimate.backgroundColor = [UIColor redColor] ;
    [self.view addSubview:_viewAnimate] ;
    CABasicAnimation *animation = [self animation] ;
    [_viewAnimate.layer addAnimation:animation forKey:@"animation.position"] ;
    _viewAnimate.layer.position = CGPointMake(250.0, 350.0) ;
    time = [_viewAnimate.layer convertTime:CACurrentMediaTime() fromLayer:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleEnterBackgroundNotification:)
                                                 name:UIApplicationDidEnterBackgroundNotification
                                               object:nil] ;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleDidBecomeActiveNotification:)
                                                 name:UIApplicationDidBecomeActiveNotification
                                               object:nil] ;

}

- (CABasicAnimation *)animation
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"] ;
    animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(50.0, 50.0)] ;
    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(250.0, 350.0)] ;
    animation.duration = 5.0 ;
    animation.delegate = self ;
    return animation ;
}

- (void)handleEnterBackgroundNotification:(id)nty
{
    CAAnimation *aa = [_viewAnimate.layer animationForKey:@"animation.position"] ;
    animationStoped = aa != nil ;
    if (animationStoped) {
        CALayer *presentationLayer = _viewAnimate.layer.presentationLayer ;
        position = presentationLayer.position ;
        CFTimeInterval now = [_viewAnimate.layer convertTime:CACurrentMediaTime() fromLayer:nil];
        time = now - time ;
    }
}

- (void)handleDidBecomeActiveNotification:(id)nty
{
    if (animationStoped) {
        CABasicAnimation *animation = [self animation] ;
        animation.fromValue = [NSValue valueWithCGPoint:position] ;
        animation.duration -= time ;
        [_viewAnimate.layer addAnimation:animation forKey:@"animation.position"] ;
    }
}
点赞