Created
May 12, 2017 10:34
-
-
Save grzegorzkrukowski/a5ed8b38bec548f9620bb95665c06128 to your computer and use it in GitHub Desktop.
Solving the problem of CALayer animations being removed when application goes to background
This file contains 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 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) | |
} | |
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() | |
} | |
} | |
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
can u update it to swift 4