4

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?

Blip
  • 1,158
  • 1
  • 14
  • 35

2 Answers2

10

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")
Blip
  • 1,158
  • 1
  • 14
  • 35
  • Isn't "OnCompletion" and "view disappearing" 2 different things? Glad that it solves the problem, but I don't understand how it actually works. – Hlung Sep 17 '21 at 05:48
0

I had the same problem, I used CABasicAnimations.

Link to doc

To combine several animations, you must use CAAnimationGroup.

Example link

mattom
  • 62
  • 10
  • I tried replacing UIView animations with CABasicAnimation. But the animation still stops the view disappears. – Blip May 05 '19 at 20:24
  • Your answer did lead to me finding the correct solution. See my answer posted next to yours. – Blip May 05 '19 at 22:35