When using this method to animate, it will stop the animation if the phone is locked then reopened or if the app is switched away from and then switched back to.
I have a StackView Titled "MainFocusStack" that I’m animating:
//AnimationSizeLOOP
UIView.animateKeyframes(withDuration: 10, delay: 0, options: [ .autoreverse, .repeat], animations: {
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 1.0) { self.mainFocusLabel.transform = CGAffineTransform(scaleX: 1.3, y: 1.3) }
})
What can I do to remedy this?
edit
This animation, and 2 others of similar code (one for glow and another for opacity), are synced with a counter. In the above code it grows for 10 seconds and falls for 10 seconds in synced with the timer.
2
Answers
You could simply start the animation when the app comes back into the foreground, e.g., let us imagine that you moved this animation code to some function:
Then you could start this not only in
viewDidLoad
, but also whenever the app becomes active, by observingUIApplication.didBecomeActiveNotification
, e.g.:If you are looking for alternative,
UIViewPropertyAnimator
can be paused and resumed, picking up from where it left off:It would be better not to want to resume the animation from where it was when it went into the background. If you really do insist on that, then it’s your responsibility to take care of this. When you go into the background, remember where you are in the animation. When you come back to the foreground, start the animation from that point of the animation.
Your "place" within the animation (often referred to as the current frame) is signified by the animated layer’s
timeOffset
, or thetimeOffset
of the animation object itself. If you’re using a UIViewPropertyAnimator, you can use itsfractionComplete
.