【问题标题】:How do I display the loading progress for a UIWebView and the actual UIWebView on separate views?如何在单独的视图上显示 UIWebView 和实际 UIWebView 的加载进度?
【发布时间】:2015-09-11 21:30:27
【问题描述】:

我希望实现一种方法,Dropbox 用于那里的 iOS 应用程序,其中他们在实际 UIWebView 显示之前显示的视图上具有 UIWebView 的实际进度视图。

The video in this link shows exactly what I am trying to say.

现在就像他们有 masterView -> progessView -> PdfViewer setup 一样,我也这样做了。所以我想问题是,他们是如何将数据从 tableView 传递到进度视图并显示 UIWebView 的?进度视图是否符合 UIWebViewDelegate 能够调用 webViewDidStartLoad(webView: UIWebView) ?并有一个代表 webView 的变量并将数据传递给 UIWebView 本身?

这是我的 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 页面?

Ant 建议将不胜感激。谢谢

【问题讨论】:

标签: ios swift uiwebview progress-bar


【解决方案1】:

你可以在这个方法中添加这种方式!

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你可以隐藏网页视图,完成加载后你可以设置隐藏为false。

希望对你有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    相关资源
    最近更新 更多