4
votes

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?

2

2 Answers

7
votes

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")
0
votes

I had the same problem, I used CABasicAnimations.

Link to doc

To combine several animations, you must use CAAnimationGroup.

Example link