gpt4 book ai didi

macos - Cocoa 中的进度条

转载 作者:行者123 更新时间:2023-12-03 17:05:24 30 4
gpt4 key购买 nike

我有一个非常简单的应用程序,其中包含 WebView。此 Web View 加载 HTML5 应用程序,并且在 Web View 中构建内容需要一些时间。

我想显示一个进度条,直到内容完成加载,并在内容准备好时显示 WebView 。大约需要 10 秒。

??

最佳答案

swift 2.2

override func viewDidLoad() {
super.viewDidLoad()

webView.frameLoadDelegate = self
webView.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil) // add observer for key path
}

/// Observer listening for progress changes
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if (keyPath == "estimatedProgress") { // listen to changes and updated view
if self.webView.estimatedProgress == 0 { return }
// All UI operations always in main thread
dispatch_async(dispatch_get_main_queue(), {
// *100 because progressIndicator in Mac OS wants values from 0 to 100
self.progressIndicator.doubleValue = self.webView.estimatedProgress * 100
})
}
}

/// Hide progress indicator on finish
func webView(sender: WebView!, didFinishLoadForFrame frame: WebFrame!) {
dispatch_async(dispatch_get_main_queue(), { self.progressIndicator.hidden = true })
}

/// Show progress indicator on start page loading
func webView(sender: WebView!, didStartProvisionalLoadForFrame frame: WebFrame!) {
dispatch_async(dispatch_get_main_queue(), { self.progressIndicator.hidden = false })
}

swift 3

override func viewWillAppear() {
super.viewWillAppear()
webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
}


override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if (keyPath == "estimatedProgress") { // listen to changes and updated view
DispatchQueue.main.async {
// Here you can do set your actions on progress update
// E.g.: someProgressBar.doubleValue = self.webView.estimatedProgress * 100
}
}
}

override func viewWillDisappear() {
super.viewWillDisappear()
webView.removeObserver(self, forKeyPath: "estimatedProgress")
}

完整解决方案

我创建了Swift 3 Cocoa code snippet使用带有嵌入式 WKWebViewController 和 NSProgressIndicator 的 NSViewController,以便您可以查看实时示例。

关于macos - Cocoa 中的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9701273/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com