Swift进度视图动画使得标准超过100%

时间:2017-06-06 18:52:19

标签: ios swift uiview animatewithduration

当为ProgressView使用动画时,如果我让动画用完但是如果我在动画仍在进行时再次尝试再次运行此代码,则它会完全正常运行,它将100%添加到当前条并使其运行通过酒吧。图像应该从更合乎逻辑的意义上解释这种情况。

进度从1.0(100%)开始 enter image description here

进展已经过了一半 Progress is half-way through

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

The code was ran again, resulting in the progress going above 100%, although it uses the correct amount of time to finish.

以下是使用的代码:

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

提前致谢!

2 个答案:

答案 0 :(得分:3)

一些快速研究......

结果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()
        })
    }

答案 1 :(得分:1)

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

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