微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如果动画尚未完成,则 UIProgressView 不会重新启动

如何解决如果动画尚未完成,则 UIProgressView 不会重新启动

我正在开发一个锻炼应用程序,我有一个 UIProgressView 在锻炼过程中充满动画。在它结束时,它会重新启动到 0 并再次填充下一次锻炼。这很有效,除非用户在动画未完成的情况下按下“下一步”。

在这种情况下,动画按预期变为 0,但直到 4-5 秒后才重新开始。

这是我的代码的样子:

self.exerciceView.progressView.setProgress(0.0,animated: false)
self.view.layoutIfNeeded()
        
startTimer()
        
UIView.animate(withDuration: TimeInterval(seconds)) {
    self.exerciceView.progressView.setProgress(1.0,animated: true)
}
self.view.layoutIfNeeded()

知道发生了什么吗?我已经被这个错误困扰了一个星期了。

谢谢!

enter image description here

解决方法

你需要

  • 删除当前动画
  • 将进度重置为 0.0
  • 强制progressView布局:

应该这样做:

    // remove animations from all layers of progressView
    if let layers = exerciceView.progressView.layer.sublayers {
        layers.forEach { layer in
            layer.removeAllAnimations()
        }
    }
    // reset to 0.0
    self.exerciceView.progressView.setProgress(0.0,animated: false)
    // force layout
    self.exerciceView.progressView.setNeedsLayout()
    self.exerciceView.progressView.layoutIfNeeded()

    // animate progressView
    UIView.animate(withDuration: TimeInterval(seconds)) {
        self.exerciceView.progressView.setProgress(1.0,animated: true)
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。