欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS 退到后台后,动画没了

程序员文章站 2022-06-18 14:24:51
0x00 动画真的没了?When my application is entering background, because the user push the home button, the animations correctly set in pause, but when i re-open my app, the animations have disappeard.How could i fix it please ?当我的应用进入了后台,因为用户按了home键,动画被设置成了暂停,但当...

0x00 动画真的没了?

When my application is entering background, because the user push the home button, the animations correctly set in pause, but when i re-open my app, the animations have disappeard.How could i fix it please ?

当我的应用进入了后台,因为用户按了home键,动画被设置成了暂停,但当我重新打开应用时,动画都消失了,我如何修复它?

This is correct and built-in behavior. When you leave the app, all animations are removed from their layers: the system calls removeAllAnimations on every layer.

你的情况是系统默认的行为.当你离开了应用后(比如进入了后台),所有的动画都从他们的layer上移除了:因为系统调用了removeAllAnimations,针对所有的layer.

引自:https://www.cnblogs.com/YouXianMing/p/3670846.html

动画其实是被移除了。

既然是被移除了
于是,我想到的方案是:重新添加回去

首先,保存了动画对象_animationGroup
接着,在开始动画时,进行判断:

    id animation = [_replayer.sublayers[0] animationForKey:kJHRadarAnimationKey];
    NSLog(@"animation:%@",animation);
    if (!animation) {
        [_replayer.sublayers[0] addAnimation:_animationGroup forKey:kJHRadarAnimationKey];
    }

非常好!
动画又回来了~


0x01 然而还有更简单的

官方文档

@interface CAAnimation : NSObject
    <NSSecureCoding, NSCopying, CAMediaTiming, CAAction>

// 省略若干...

/* When true, the animation is removed from the render tree once its
 * active duration has passed. Defaults to YES. */

@property(getter=isRemovedOnCompletion) BOOL removedOnCompletion;

@end

这个属性removedOnCompletion 默认是 YES
这个是在动画完成后,自动移除动画。

于是,把这个属性removedOnCompletion 设置为 NO
退到后台
再重新进入
动画依旧还在
非常好!


0x02 这究竟是一个怎样的动画?

https://github.com/xjh093/JHRadarAnimation


本文地址:https://blog.csdn.net/xjh093/article/details/108132554

相关标签: iOS