天天看點

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.​​      

動畫其實是​

​被移除​

​了。

既然是被移除了

于是,我想到的方案是:​​

​重新添加回去​

首先,儲存了動畫對象​

​_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​

​ 這個是在動畫完成後,自動移除動畫。

0x02 這究竟是一個怎樣的動畫?