【问题标题】:Swift Progress View animation makes the bar go further than 100%Swift Progress View 动画使进度条超过 100%
【发布时间】:2017-06-06 18:52:18
【问题描述】:

当为 ProgressView 使用动画时,如果我让动画用完,它完全可以正常工作,但如果我在动画仍在运行时再次尝试运行此代码,它会将 100% 添加到当前栏并使其运行通过酒吧。图片应该更合乎逻辑地解释这种情况。

进度从 1.0 (100%) 开始:

进度已过半:

代码再次运行,导致进度超过 100%,尽管它使用了正确的时间来完成。

这是使用的代码:

self.progressView.setProgress(1.0, animated: false)
        
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  UIView.animate(withDuration: 10, delay: 0, options: [.beginFromCurrentState, .allowUserInteraction], animations: { [unowned self] in
    self.progressView.setProgress(0, animated: true)
  })
}

提前致谢!

【问题讨论】:

  • 您正在运行的其余代码(在设置下一个问题时)是否有可能无意中更改了进度视图的框架或约束?

标签: ios swift uiview animatewithduration


【解决方案1】:

一些快速研究...

原来UIProgressView 需要一些特殊的努力来停止当前的动画。看看这是否适合你:

    // stop any current animation
    self.progressView.layer.sublayers?.forEach { $0.removeAllAnimations() }

    // reset progressView to 100%
    self.progressView.setProgress(1.0, animated: false)

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
        // set progressView to 0%, with animated set to false
        self.progressView.setProgress(0.0, animated: false)
        // 10-second animation changing from 100% to 0%
        UIView.animate(withDuration: 10, delay: 0, options: [], animations: { [unowned self] in
            self.progressView.layoutIfNeeded()
        })
    }

【讨论】:

  • 它适用于所有值,除了 0 - 它没有动画就消失了......
【解决方案2】:

setProgress(_:animated:) 方法已经为您处理了动画。当animated参数设置为true时,进度变化将被动画化。

尝试不使用动画块:

self.progressView.setProgress(1.0, animated: false)

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
    self.progressView.setProgress(0.0, animated: true)
}

还要确保这不是自动布局问题。检查您对进度视图的约束并确保其宽度得到适当的约束。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 2014-04-23
    • 1970-01-01
    相关资源
    最近更新 更多