【发布时间】:2017-01-27 16:25:55
【问题描述】:
我尝试在我的应用程序上添加进度条。我在How to add Progress bar to UIAlertController? 上找到了这个问题,但它没有显示如何更新进度条。我将代码简化如下,但它没有更新进度条(仅在进度完成后才显示)。我看了什么?感谢您的帮助。
override func viewDidLoad() {
super.viewDidLoad()
var topViewController = UIApplication.shared.delegate!.window!!.rootViewController!
while (topViewController.presentedViewController != nil){
topViewController = topViewController.presentedViewController!
}
DispatchQueue.main.async(execute: {
let alert = UIAlertController(title: "downloading", message: "pls wait", preferredStyle: .alert)
let progressBar = UIProgressView(progressViewStyle: .default)
progressBar.setProgress(0.0, animated: true)
progressBar.frame = CGRect(x: 10, y: 70, width: 250, height: 0)
alert.view.addSubview(progressBar)
topViewController.present(alert, animated: true, completion: nil)
var progress: Float = 0.0
repeat {
DispatchQueue.global(qos: .background).async(execute: {
progress += 0.01
print (progress)
DispatchQueue.main.async(flags: .barrier, execute: {
progressBar.setProgress(progress, animated: true)
})
})
} while progress < 1.0
})
}
【问题讨论】:
-
进度循环的运行速度有多快?您是否看到打印的进度缓慢增加,或者所有数字都在瞬间出现?在那里添加一点延迟,加载文件或其他内容,然后看看它的外观
标签: ios swift3 queue progress-bar uialertcontroller