I have a iOS application with a tab bar at the bottom, like this:
The white circle at the center of the tab bar pulsates by fading in and out repeatedly. Here's the code that does the pulsate animation:
UIView.animateKeyframes(withDuration: 1.4, delay: 0, options: [.repeat, .autoreverse], animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.7, animations: {
self.recordingPulsatingCircleView?.alpha = 1
})
UIView.addKeyframe(withRelativeStartTime: 0.7, relativeDuration: 1.4, animations: {
self.recordingPulsatingCircleView?.alpha = 0
})
}, completion: nil)
The problem is that when the tab bar disappears, by, for example, being hidden behind another view, or when I click the home button and bring back the app again, the animation stops, and the white circle disappears, like this:
I expect it to continue animating because I set .repeat
as one of the options
. Any help?
I solved my problem by replacing the UIView.animateKeyframes
with CABasicAnimation
, then setting the property isRemovedOnCompletion
of CABasicAnimation
to false
. This way the animation no longer stops when the view is placed out of the screen. Here's the code:
let animation = CABasicAnimation(keyPath: "opacity")
animation.fromValue = 0
animation.toValue = 1
animation.duration = 0.7
animation.autoreverses = true
animation.repeatCount = .infinity
animation.isRemovedOnCompletion = false //Set this property to false.
recordingPulsatingCircleView?.layer.add(animation, forKey: "pulsating")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With