当 UIView 消失时 UIView 重复动画停止

Bli*_*lip 4 animation ios swift

我有一个底部带有标签栏的 iOS 应用程序,如下所示: 标签栏

标签栏中心的白色圆圈通过反复淡入淡出而脉动。这是执行脉动动画的代码:

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)
Run Code Online (Sandbox Code Playgroud)

问题是,当标签栏消失时,例如,隐藏在另一个视图后面,或者当我单击主页按钮并再次返回应用程序时,动画停止,白色圆圈消失,如下所示: 在此处输入图片说明

我希望它继续动画,因为我设置.repeatoptions. 有什么帮助吗?

Bli*_*lip 7

我通过更换解决我的问题UIView.animateKeyframesCABasicAnimation,然后设置该属性isRemovedOnCompletionCABasicAnimationfalse。这样当视图被放置在屏幕之外时动画不再停止。这是代码:

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")
Run Code Online (Sandbox Code Playgroud)