当NSProgressIndicator
采用spinstyle
时,如果还有后续的操作,系统会优先执行后续操作,在操作结束后,才运行状态条。这是我们所不希望的。此时需要暂时停止RunLoop.main
,待状态条开始运行后,再进行后续的工作。
func userDefaultsDidChange() {
guard let controller = tableViewController else { return }
let progressIndicator = { () -> NSProgressIndicator in
let frame = view.frame
let x = (frame.width - 50) / 2
let y = (frame.height - 50) / 2
let piFrame = NSMakeRect(x, y, 50, 50)
let pi = NSProgressIndicator(frame: piFrame)
pi.style = .spinningStyle
return pi
}()
view.addSubview(progressIndicator)
progressIndicator.startAnimation(self)
RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.001))
controller.run()
progressIndicator.stopAnimation(self)
progressIndicator.removeFromSuperview()
}