将进度条添加到UIAlertController并显示更新

时间:2017-01-27 16:25:55

标签: ios swift3 queue progress-bar uialertcontroller

我尝试在我的应用程序上添加进度条。我在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
    })
}

2 个答案:

答案 0 :(得分:5)

您的代码中的所有调度队列都会让您感到困惑: - )

我试着解释这个问题:

第一个(最外面的) DispatchQueue.main.async在主(UI)主题中有效。 它会创建UIAlertController,将UIProgressView填入其中并显示对话框。然后它在主线程中执行 一些时间关键的工作(循环重复)。永远不要这样做,因为阻止主线程。 由于主线程被阻止,因此UI控件中不会反映任何更新,因此您看不到进度更改。

因此,

  • 我将时间关键工作项(重复循环)作为异步工作项放入全局队列(不是主队列)
    • 在该工作项中,我将进度条更新发送到主队列(将在主线程中执行)
    • 最后,当一切都完成后,我会忽略UIAlertController

当您从viewDidLoadviewWillAppear等方法显示提醒视图时,您只需要第一次调度,例如从尚未显示视图的时间点开始。如果您通过按钮回调或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);
            })
        })

    })
}