肇鑫的技术博客

业精于勤,荒于嬉

ProgressBar与进度

// MARK: - progress bar with file dealing process
private var counter:Int = 0

func process() {
    // get total

    // prepare alert
    let screenFrame = NSScreen.main()!.frame
    let window = NSWindow(contentRect: NSMakeRect(screenFrame.width / 2 - 140, screenFrame.height * 0.66 + 50, 280, 20),
                          styleMask: NSBorderlessWindowMask,
                          backing: .buffered, defer: false)
    window.isOpaque = false

    let alert = NSAlert()

    alert.addButton(withTitle: "OK")
    alert.buttons.first!.isHidden = true

    // prepare prgreessBar
    let progressBar = NSProgressIndicator(frame: NSMakeRect(0,0,296,20))
    progressBar.isIndeterminate = false
    progressBar.maxValue = 1000
    progressBar.doubleValue = 0
    let step = progressBar.maxValue / Double(total)
    alert.accessoryView = progressBar
    alert.messageText = "0/\(total)"
    alert.beginSheetModal(for: window) { [unowned self] (buttonId) in
        alert.window.orderOut(self)
    }

    let userInfo:[String:Any] = ["progress bar": progressBar, "step": step, "alert": alert, "total": total]

    // timer to update progress bar UI
    self.counter = 0
    let timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(updateProgressbar(timer:)), userInfo: userInfo, repeats: true)

    DispatchQueue.global().async() { [unowned self] () -> () in
        func excute() {
            // MARK: - do works
            // for in loop 
            for _ in 0 ... total {
                // update counter
                self.counter += 1
            }
        }

        excute()

        DispatchQueue.main.async { [unowned self] () -> () in
            timer.invalidate()
            progressBar.doubleValue = 1000
            alert.messageText = "\(total)/\(total)"

            alert.buttons.first!.performClick(self)
        }
    }
}

func updateProgressbar(timer:Timer) {
    DispatchQueue.main.async { [unowned self] () -> () in
        if let dic = timer.userInfo as? [String:Any], let progressBar = dic["progress bar"] as? NSProgressIndicator, let step = dic["step"] as? Double, let alert = dic["alert"] as? NSAlert, let total = dic["total"] as? Int {
            progressBar.doubleValue = step * Double(self.counter)
            alert.messageText = "\(self.counter)/\(total)"
        }
    }
}

有趣的是,hidden的按钮在程序里是可以点击的。

另外,必须通过模拟点击的方式才能正确退出alert。采用其它方式有可能造成UI异常,甚至NSWindow不能正确释放而导致程序退出时崩溃。