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的更多相关文章

  1. iOS-WebView(WKWebView)进度条

    一直以来,就有想通过技术博客来记录总结下自己工作中碰到的问题的想法,这个想法拖了好久今天才开始着手写自己的第一篇技术博客,由于刚开始写,不免会出现不对的地方,希望各位看到的大牛多多指教.好了,不多说了 ...

  2. iOS8 无缝切换WKWebView,借鉴IMYWebview,解决进度条,cookie,本地页面等问题

    webkit使用WKWebView来代替IOS的UIWebView和OSX的WebView,并且使用Nitro JavaScript引擎,这意味着所有第三方浏览器运行JavaScript将会跟safa ...

  3. iOS WKWebView添加进度条02

    之前写了一个是关于webview添加进度条的,现在补一个WKWebView进度条. //添加一个全局属性 @property(nonatomic,strong)CALayer *progresslay ...

  4. 《动手实现一个网页加载进度loading》

    loading随处可见,比如一个app经常会有下拉刷新,上拉加载的功能,在刷新和加载的过程中为了让用户感知到 load 的过程,我们会使用一些过渡动画来表达.最常见的比如"转圈圈" ...

  5. java进行文件上传,带进度条

    网上看到别人发过的一个java上传的代码,自己写了个完整的,附带源码 项目环境:jkd7.tomcat7. jar包:commons-fileupload-1.2.1.jar.commons-io-1 ...

  6. Bootstrap <基础二十六>进度条

    Bootstrap 进度条.在本教程中,你将看到如何使用 Bootstrap 创建加载.重定向或动作状态的进度条. Bootstrap 进度条使用 CSS3 过渡和动画来获得该效果.Internet ...

  7. JQuery入门——进度条

    越来越觉得常规javascript已经跟不上节奏了,打算学点进阶的,从JQuery学起. JQuery是一个Javascript库,可以从JQuery.com下载,放到本地,用 <script ...

  8. JS网页顶部进度条demo

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  9. 原生javascript模仿win8等待进度条。

    一.序言 一直很中意win8等待提示圆圈进度条.win8刚出来那会,感觉好神奇!苦于当时没思路,没去研究.通过最近网上找找资料,终于给搞出来了!先上Demo,献丑了!预览请看:win8进度条. 二.简 ...

随机推荐

  1. SpringSecurity 3.2入门(6)简单介绍默认使用的十一个过滤器

    Security提供了20多个filter,每个过滤器都提供特定的功能.这些filter在Spring Security filter过滤器链中的缺省顺序由 org.springframework.s ...

  2. 02.switch的使用

    基本语法: switch-case语法: switch(表达式/变量) { case 值1: 语句块1; break; case 值2: 语句块2; break; default:语句块3; brea ...

  3. PHP file_put_contents() 函数

    file_put_contents() 函数把一个字符串写入文件中. 与依次调用 fopen(),fwrite() 以及 fclose() 功能一样. 语法如下 file_put_contents(f ...

  4. POJ 3667 线段树区间合并

    http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html 用线段树,首先要定义好线段树的节点信息,一般看到一个问题,很难很 ...

  5. 深入理解java线程池—ThreadPoolExecutor

    几句闲扯:首先,我想说java的线程池真的是很绕,以前一直都感觉新建几个线程一直不退出到底是怎么实现的,也就有了后来学习ThreadPoolExecutor源码.学习源码的过程中,最恶心的其实就是几种 ...

  6. 9th week -the history of program 1950~2020

    We already know that programming language is a formal language designed to communicate instructions ...

  7. XMLHttpRequest.responseType

    "arraybuffer" "blob" "document" "json" "text"

  8. (五)TortoiseSVN 客户端-----安装

    svn客户端类型 svn客户端需要通过网络访问svn服务端提交文件.查询文件等,可通过以下客户端类型访问svn服务端: 使用Subversion提供的客户端命令,使用方式:在命令行下输入命令操作. 使 ...

  9. 【Leetcode】【Easy】Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  10. 一键生成http服务器

    如果你想用最简单的方法在内网共享目录,可以考虑为要共享的目录生成一个http服务器,这样就可以在内网任一台设备打开浏览器就可以浏览了.简单举几个例,有了这个http服务器就可以: 在手机浏览器里观看电 ...