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

4
我有一个iOS应用,底部有一个类似这样的选项卡: tab bar 选项卡底部的白色圆圈会通过不断淡入淡出而脉动。以下是执行脉动动画的代码:
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)

问题出在当标签栏消失时,比如被隐藏在另一个视图后面,或者当我点击Home键再次打开应用时,动画停止了,白色圆圈也消失了,就像这样: enter image description here 我预期它能够继续播放动画,因为我将 .repeat 设置为其中之一的 options。有任何帮助吗?
2个回答

11

我通过用CABasicAnimation替换UIView.animateKeyframes,然后将CABasicAnimationisRemovedOnCompletion属性设置为false来解决了我的问题。这样,当视图移出屏幕时,动画就不会再停止了。以下是代码:

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

“OnCompletion”和“视图消失”不是两回事吗?很高兴它解决了问题,但我不明白它是如何工作的。 - Hlung

0

我曾经遇到过同样的问题,我使用了CABasicAnimations。

文档链接

如果要组合多个动画,必须使用CAAnimationGroup。

示例链接


我尝试用CABasicAnimation替换UIView动画。但是动画仍然停止,视图消失了。 - Blip
你的回答确实帮助我找到了正确的解决方案。请看我在你旁边发布的答案。 - Blip

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接