【问题标题】:UIView repeating animation stops when UIView disappears当 UIView 消失时 UIView 重复动画停止
【发布时间】:2019-09-23 12:26:46
【问题描述】:

我有一个底部有标签栏的 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)

问题是当标签栏消失时,例如隐藏在另一个视图后面,或者当我单击主页按钮并再次带回应用程序时,动画停止,白色圆圈消失,像这样:

我希望它继续制作动画,因为我将.repeat 设置为options 之一。有什么帮助吗?

【问题讨论】:

    标签: ios swift animation


    【解决方案1】:

    我解决了我的问题,将UIView.animateKeyframes 替换为CABasicAnimation,然后将CABasicAnimation 的属性isRemovedOnCompletion 设置为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”和“视图消失”不是两个不同的东西吗?很高兴它解决了问题,但我不明白它实际上是如何工作的。
    【解决方案2】:

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

    Link to doc

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

    Example link

    【讨论】:

    • 我尝试用 CABasicAnimation 替换 UIView 动画。但动画仍然停止视图消失。
    • 您的回答确实让我找到了正确的解决方案。看看我的答案贴在你的旁边。
    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多