iOS 退到后台后,动画没了

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豪冷啊

你的鼓励是对我的认可!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值