Forked from grzegorzkrukowski/CALayer+Pause.swift
Last active
April 18, 2018 16:07
-
-
Save etiennepinchon/c8db656abc6793d7d728d5d795d2bee6 to your computer and use it in GitHub Desktop.
Resume UIAnimation after app entered background - Updated to Swift 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // CALayer+Pause.swift | |
| // | |
| // Created by Grzegorz Krukowski on 11/05/2017. | |
| // Copyright © 2017. All rights reserved. | |
| // | |
| import Foundation | |
| extension CALayer { | |
| func pause() { | |
| if self.isPaused() == false { | |
| let pausedTime: CFTimeInterval = self.convertTime(CACurrentMediaTime(), from: nil) | |
| self.speed = 0.0 | |
| self.timeOffset = pausedTime | |
| } | |
| } | |
| func isPaused() -> Bool { | |
| return self.speed == 0.0 | |
| } | |
| func resume() { | |
| let pausedTime: CFTimeInterval = self.timeOffset | |
| self.speed = 1.0 | |
| self.timeOffset = 0.0 | |
| self.beginTime = 0.0 | |
| let timeSincePause: CFTimeInterval = self.convertTime(CACurrentMediaTime(), from: nil) - pausedTime | |
| self.beginTime = timeSincePause | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // ViewWithPersistentAnimations.swift | |
| // | |
| // Created by Grzegorz Krukowski on 12/05/2017. | |
| // Copyright © 2017. All rights reserved. | |
| // | |
| /* Solving the problem of CALayers animations being lost when application is going to background */ | |
| import Foundation | |
| class ViewWithPersistentAnimations : UIView { | |
| private var persistentAnimations: [String: CAAnimation] = [:] | |
| private var persistentSpeed: Float = 0.0 | |
| override init(frame: CGRect) { | |
| super.init(frame: frame) | |
| self.commonInit() | |
| } | |
| required init?(coder aDecoder: NSCoder) { | |
| super.init(coder: aDecoder) | |
| self.commonInit() | |
| } | |
| func commonInit() { | |
| NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil) | |
| NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil) | |
| } | |
| deinit { | |
| NotificationCenter.default.removeObserver(self) | |
| } | |
| @objc func didBecomeActive() { | |
| self.restoreAnimations(withKeys: Array(self.persistentAnimations.keys)) | |
| self.persistentAnimations.removeAll() | |
| if self.persistentSpeed == 1.0 { //if layer was plaiyng before backgorund, resume it | |
| self.layer.resume() | |
| } | |
| } | |
| @objc func willResignActive() { | |
| self.persistentSpeed = self.layer.speed | |
| self.layer.speed = 1.0 //in case layer was paused from outside, set speed to 1.0 to get all animations | |
| self.persistAnimations(withKeys: self.layer.animationKeys()) | |
| self.layer.speed = self.persistentSpeed //restore original speed | |
| self.layer.pause() | |
| } | |
| func persistAnimations(withKeys: [String]?) { | |
| withKeys?.forEach({ (key) in | |
| if let animation = self.layer.animation(forKey: key) { | |
| self.persistentAnimations[key] = animation | |
| } | |
| }) | |
| } | |
| func restoreAnimations(withKeys: [String]?) { | |
| withKeys?.forEach { key in | |
| if let persistentAnimation = self.persistentAnimations[key] { | |
| self.layer.add(persistentAnimation, forKey: key) | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment