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. C# 创建一个WCF服务

    做代码统计,方便以后使用: app.config配置文件设置: <configuration> <system.serviceModel> <bindings> & ...

  2. java使用commons-fileupload进行文件上传

    java中使用文件上传时需要使用特定的类库,这里使用commons-files类库进行文件上传,在http://commons.apache.org/proper/commons-fileupload ...

  3. Vue element-ui:滚动条 分页 禁用选项

    1.滚动条设置: <el-scrollbar style="height:100%;">.......</el-scrollbar> 默认会同时出现水平和垂 ...

  4. vuex深入浅出

    本文主要记录使用vuex的使用场景.重要组成部分和学习心得. 1.说在前面 学习vue有两周的时间了,目前已经对vue的基础使用比较熟悉了.但是一直对vuex的使用耿耿于怀,这么说是因为总是不太理解, ...

  5. iframe中插入代码并执行

    最近有需求通过iframe插入代码.有蛮多方法,如下: 1 var iframe = document.getElementById('previewUrl'); 2 iframe.src = 'ab ...

  6. 用js md5加密

    /* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as d ...

  7. Mybatis 的多个数据源

    主要思路: DataSource --> SqlSessionFactory --> SqlSessionTemplate 第一方式: 定义多套  DataSource --> Sq ...

  8. selenium(2.4.0)中不能导出web drive 代码

    最近在学习selenium.要做web自动化测试.咱只会C# .就想了,这个测试用例要是能用C# 控制,保存起来,就可以以后自动运行了,不用每次点击运行了. 看了看它的文档,就开始试验了,录脚本折腾了 ...

  9. C#发送电子邮件 (异步) z

    ///验证电子邮件的正则表达式   string emailStr = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([ ...

  10. Apache常见问题

    Apache如何修改端口? 找到Apache安装目录,conf目录下的httpd.conf文件,用编辑器打开. 找到“Listen 80”,修改为我们想要的端口号就可以了,如“Listen 8080” ...