UIProgressView 不显示进度 (iOS)

UIProgressView doesn't show progress (iOS)

我有一个使用 iOSDFULibrary.

更新 BLE 设备的应用程序

我有这个功能:

   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
    }