我尝试在我的应用程序上添加进度条。我在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
})
}
答案 0 :(得分:5)
您的代码中的所有调度队列都会让您感到困惑: - )
我试着解释这个问题:
第一个(最外面的) DispatchQueue.main.async
在主(UI)主题中有效。
它会创建UIAlertController
,将UIProgressView
填入其中并显示对话框。然后它在主线程中执行 一些时间关键的工作(循环重复)。永远不要这样做,因为阻止主线程。
由于主线程被阻止,因此UI控件中不会反映任何更新,因此您看不到进度更改。
因此,
UIAlertController
当您从viewDidLoad
或viewWillAppear
等方法显示提醒视图时,您只需要第一次调度,例如从尚未显示视图的时间点开始。如果您通过按钮回调或viewDidAppear
(例如视图可见)来调用它,则可以跳过该外部调度。
在这里 - 我也进入了一些休眠时间并修改了GCD-Calls以便使用尾随闭包:
override func viewDidLoad() {
super.viewDidLoad()
var topViewController = UIApplication.shared.delegate!.window!!.rootViewController!
while (topViewController.presentedViewController != nil){
topViewController = topViewController.presentedViewController!
}
// Usage of GCD here is only necessary because it's called from
// viewDidLoad. If called by viewDidAppear or some action callback, you
// don't need it:
DispatchQueue.main.async {
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
// Do the time critical stuff asynchronously
DispatchQueue.global(qos: .background).async {
repeat {
progress += 0.1
Thread.sleep(forTimeInterval: 0.25)
print (progress)
DispatchQueue.main.async(flags: .barrier) {
progressBar.setProgress(progress, animated: true)
}
} while progress < 1.0
DispatchQueue.main.async {
alert.dismiss(animated: true, completion: nil);
}
}
}
}
答案 1 :(得分:0)
在安德烈亚斯的帮助下发表评论。我正在研究这段代码。
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: "loading", message: "wait please", preferredStyle: .alert)
let rect = CGRect(x: 10, y: 70, width: 250, height: 0)
let progressView = UIProgressView(frame: rect)
progressView.progress = 0.0
progressView.tintColor = UIColor.blue
alert.view.addSubview(progressView)
topViewController.present(alert, animated: true, completion: nil)
var progress = 0.0
DispatchQueue.global(qos: .background).async(execute: {
repeat {
progress += 0.0001
print (progress)
DispatchQueue.main.async(flags: .barrier, execute: {
progressView.progress = Float(progress)
})
} while progress < 1.0
DispatchQueue.main.async(execute: {
alert.dismiss(animated: true, completion: nil);
})
})
})
}