设为首页 加入收藏

TOP

WKWebView进度及title(一)
2017-10-10 12:14:18 】 浏览:8346
Tags:WKWebView 进度 title

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)observeva lueForKeyPath:(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 observeva lueForKeyPath:keyPath ofObject:object change:change context:context];

        }

 

    }

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

    {

        if (object == self.webView) {

            self.title = self.webView.title;

 

        }

        else

        {

            [super observeva lueForKeyPath:keyPath ofObject:object change:change context:context];

 

        }

    }

    else {

 

        [super observeva lueForKeyPath: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 observeva lueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {

        if keyPath == "estimated

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇SWIFT推送之本地推送(UILocalNoti.. 下一篇app让个别界面横屏,其他的为竖屏..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目