繁体   English   中英

UIProgressView 不显示进度(iOS)

[英]UIProgressView doesn't show progress (iOS)

我有一个使用iOSDFULibrary更新 BLE 设备的应用程序。

我有这个 function:

   func dfuProgressDidChange(for part: Int, outOf totalParts: Int, to progress: Int, currentSpeedBytesPerSecond: Double, avgSpeedBytesPerSecond: Double) {
       print("\t\(part)\t\(totalParts)\t\(progress)\t\(currentSpeedBytesPerSecond)\t\(avgSpeedBytesPerSecond)")
}

当更新正在进行时,我希望我的 UIProgressView 相应地移动progress并在进度达到 100 时完全填充。

到目前为止,我所拥有的是:

@IBOutlet weak var progressView: UIProgressView!


progressView.progressViewStyle = .default
progressView.tintColor = .orange
progressView.progressTintColor = .orange
progressView.backgroundColor = .none
progressView.progress = Float(progress)
progressView.setProgress(100.0, animated: true)
func dfuProgressDidChange(for part: Int, outOf totalParts: Int, to progress: Int, currentSpeedBytesPerSecond: Double, avgSpeedBytesPerSecond: Double) {
    let progress = Float(part) / Float(total)
    progressView.setProgress(progress, animated: true)
}

我还注意到您为 ProgressView 设置了 100 个进度。

progressView.setProgress(100.0, animated: true)

ProgressView 最大进度为 1.0

open class UIProgressView : UIView, NSCoding {

    open var progress: Float // 0.0 .. 1.0, default is 0.0. values outside are pinned.

}

事实证明,我必须避免在我的代码中添加的是:

progressView.setProgress(100.0, animated: true)

我删除了它。 前面提到的最大值是1.0 ,我的进度是 0-100。 所以,为了让progressView显示变化,我必须首先将我的进度转换为Float ,然后将其除以 100,所以我们的最大值是 1.0 而不是 100:

progressView.progress = Float(progress)/100

所以,现在我的代码如下所示:

@IBOutlet weak var progressView: UIProgressView!


progressView.progressViewStyle = .default
progressView.tintColor = .orange
progressView.progressTintColor = .orange
progressView.backgroundColor = .none

    func dfuProgressDidChange(for part: Int, outOf totalParts: Int, to progress: Int, currentSpeedBytesPerSecond: Double, avgSpeedBytesPerSecond: Double) {
        print("\t\(part)\t\(totalParts)\t\(progress)\t\(currentSpeedBytesPerSecond)\t\(avgSpeedBytesPerSecond)")
        
        progressView.progress = Float(progress)/100
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM