Reputation: 724
I'm infinitely looping a UIView animation with the following
UIView.animate(withDuration: 1.0, delay: 0, options: [.autoreverse, .repeat], animations: {
self.someLabel.alpha = 0.3
}, completion: nil)
This works fine but when the viewController wakes up the animation freezes where it is.
Running the same code as above in viewDidWakeUp()
doesn't fix it.
How can I make the animation either not freeze, or continue where it left off when the viewController wakes up.
To clarify, by 'wake up' I mean either of the following:
Upvotes: 1
Views: 652
Reputation: 2252
Add two notification willEnterForegroundNotification and didEnterBackgroundNotification.
It is also worth noting. That in some cases, you need to reset the animated property to get the new animation to stick. I can confirm this with an animated transformation.
Just calling...
view.layer.removeAllAnimations()
self.someLabel.alpha = 1.0
//Complete code
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NotificationCenter.default.addObserver(self, selector:#selector(didEnterForeground) , name: UIApplication.willEnterForegroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector:#selector(didEnterBackground) , name: UIApplication.didEnterBackgroundNotification, object: nil)
}
@objc func didEnterBackground() {
view.layer.removeAllAnimations()
self.someLabel.alpha = 1.0
}
@objc func didEnterForeground() {
DispatchQueue.main.async {
self.animation()
}
}
func animation() {
UIView.animate(withDuration: 1.0, delay: 0, options: [.autoreverse, .repeat], animations: {
self.someLabel.alpha = 0.3
}, completion: nil)
}
Upvotes: 1