Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView repeating animation stops when UIView disappears

I have a iOS application with a tab bar at the bottom, like this: tab bar

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: enter image description here

I expect it to continue animating because I set .repeat as one of the options. Any help?

like image 319
Blip Avatar asked Jan 27 '23 09:01

Blip


1 Answers

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")
like image 171
Blip Avatar answered Feb 06 '23 23:02

Blip