0

我有一个使用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)
4

2 回答 2

1
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.

}
于 2020-06-26T13:53:39.717 回答
-1

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

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
    }
于 2020-06-30T07:21:59.923 回答