WKWebView進度及title

來源:互聯網
上載者:User

標籤:

WKWebView進度及title

WKWebView進度及title

WKWebView 的estimatedProgress和title 都是KVO模式,所以可以添加監控:

    [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];

    [webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];

  •  

監控的實現方法:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

 

    if ([keyPath isEqualToString:@"estimatedProgress"]) {

 

        if (object == webView) {

            [self.progressView setAlpha:1.0f];

            [self.progressView setProgress:self.currentSubView.webView.estimatedProgress animated:YES];

 

            if(self.currentSubView.webView.estimatedProgress >= 1.0f) {

 

                [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{

                    [self.progressView setAlpha:0.0f];

                } completion:^(BOOL finished) {

                    [self.progressView setProgress:0.0f animated:NO];

                }];

 

            }

        }

        else

        {

            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

        }

 

    }

    else if ([keyPath isEqualToString:@"title"])

    {

        if (object == self.webView) {

            self.title = self.webView.title;

 

        }

        else

        {

            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

 

        }

    }

    else {

 

        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

    }

}

  •  

這裡的進度增加了動畫,類似safari的進度效果

需要注意的是銷毀的時候一定要移除監控

        [webView removeObserver:self forKeyPath:@"estimatedProgress"];

        [webView removeObserver:self forKeyPath:@"title"];

 

swift:

// 監聽

        theWebView?.addObserver(self, forKeyPath: "estimatedProgress", options: NSKeyValueObservingOptions.New, context: nil)

        theWebView?.addObserver(self, forKeyPath: "title", options: NSKeyValueObservingOptions.New, context: nil)

 

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {

        if keyPath == "estimatedProgress" {

 

            if ((object?.isEqual(theWebView)) != false) {

                 self.progressView.alpha = 1.0

                self.progressView.setProgress(Float((self.theWebView?.estimatedProgress)!), animated: true)

 

                if self.theWebView?.estimatedProgress >= 1.0 {

 

                    UIView.animateWithDuration(0.3, delay: 0.3, options: .CurveEaseOut, animations: {

                        self.progressView.alpha = 0.0

                        }, completion: { (finished) in

                            self.progressView.setProgress(0.0, animated: false)

                    })

                }

            }else {

                super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)

            }

        }

    }

    deinit {

        webView.removeObserver(self, forKeyPath: "estimatedProgress")

        webView.removeObserver(self, forKeyPath: "title")

    }

WKWebView進度及title

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.