如何在单独的视图上显示 UIWebView 和实际 UIWebView 的加载进度? [英] How do I display the loading progress for a UIWebView and the actual UIWebView on separate views?
问题描述
我希望实现一种 Dropbox 用于 iOS 应用程序的方法,在该方法中,他们在显示实际 UIWebView 之前显示的视图上具有 UIWebView 的实际进度视图.
Im looking to implement a method that dropbox uses for there iOS app, where they have the actual progress view for a UIWebView on a view that is shown right before the actual UIWebView is shown.
现在就像他们有 masterView -> progessView -> PdfViewer setup 一样,我也有.所以我想问题是,他们是如何将数据从 tableView 传递到进度视图并显示 UIWebView 的?进度视图是否符合 UIWebViewDelegate 能够调用 webViewDidStartLoad(webView: UIWebView)
?并有一个代表 webView 的变量并将数据传递给 UIWebView 本身?
Now just as they have a masterView -> progessView -> PdfViewer setup , I do as well. So I guess the question is, how did they pass the data from the tableView, onto the progress view, and display the UIWebView? Does the progress view conform to the UIWebViewDelegate to be able to call webViewDidStartLoad(webView: UIWebView)
? and have a variable representing the webView and pass along the data to the UIWebView itself?
这是我的 UIWebView 的代码:
Here is the code for my UIWebView:
class PdfViewController: UIViewController, UIWebViewDelegate {
@IBOutlet var myWebView: UIWebView!
@IBOutlet var progressView: UIProgressView!
var hasFinishedLoading = false
var selectedContent: String!
override func viewDidLoad() {
super.viewDidLoad()
myWebView.delegate = self
let url: NSURL! = NSURL(string: selectedContent)
myWebView.loadRequest(NSURLRequest(URL: url))
}
func webViewDidStartLoad(webView: UIWebView) {
var hasFinishedLoading = false
updateProgress()
}
func webViewDidFinishLoad(webView: UIWebView) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))),
dispatch_get_main_queue()) {
[weak self] in
if let _self = self {
_self.hasFinishedLoading = true
}
}
}
deinit {
myWebView.stopLoading()
myWebView.delegate = nil
}
func updateProgress(){
if progressView.progress >= 1 {
progressView.hidden = true
} else {
if hasFinishedLoading {
progressView.progress += 0.002
} else {
if progressView.progress <= 0.3 {
progressView.progress += 0.004
} else if progressView.progress <= 0.6 {
progressView.progress += 0.002
} else if progressView.progress <= 0.9 {
progressView.progress += 0.001
} else if progressView.progress <= 0.94 {
progressView.progress += 0.0001
} else {
progressView.progress = 0.9401
}
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.008 * Double(NSEC_PER_SEC))),
dispatch_get_main_queue()) {
[weak self] in
if let _self = self {
_self.updateProgress()
}
}
}
}
}
现在我该如何获取这段代码,并以某种方式将其传递给我的 progessViewController 以让它显示进度而不是显示在我的 WebView 中?当进度完成时,比实例化并显示加载的 UIWebView 页面?
Now how do I take this code, and instead pass it to my progessViewController somehow to let it show the progress rather than it being shown in my WebView? and when the progress complete, than instantiate and show the loaded UIWebView page?
Ant 的建议将不胜感激.谢谢
Ant suggestions would be appreciated. Thanks
推荐答案
你可以在这个方法中添加这个方法!
You can add this way in this method!
func webViewDidFinishLoad(webView: UIWebView) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))),
dispatch_get_main_queue()) {
[weak self] in
if let _self = self {
_self.hasFinishedLoading = true
//Add here!
_self.view.addSubview(webView)
}
}
}
您可以采用其他方法,在 viewWillAppear
中您可以隐藏 web 视图,完成加载后您可以设置 hidden 为 false.
You can do other approach, in viewWillAppear
you can hide the web view and when finish load you can set hidden false.
希望能帮到你!
这篇关于如何在单独的视图上显示 UIWebView 和实际 UIWebView 的加载进度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!