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")

}

转载于:https://www.cnblogs.com/YangFuShun/p/5718872.html

WKWebView进度及title相关推荐

  1. iOS实现微信网页进度条,WKWebview进度条的实现

    实现微信网页进度条或者说就是webview的加载进度条的效果,主要代码如下: 一.第一步,添加观察者 [_webViewaddObserver:selfforKeyPath:@"estima ...

  2. Vml+Dhtml:制作一个应用渐变颜色效果不错的进度条

    //原作:风云舞,载自: http://www.lshdic.com/bbs <HTML xmlns:v> <HEAD> <META http-equiv='Conten ...

  3. css实现横向进度条和竖向进度条

    一.横向进度条 <html> <head> <title>横向进度条</title> <style type="text/css&quo ...

  4. EasyUI中进度条的简单使用

    场景 效果 属性 名称 类型 描述 默认值 width string 设置进度条(progressbar)的宽度. auto height number 组件的高度.该属性自版本 1.3.2 起可用. ...

  5. html video 设置进度条不可拖动_PHP大文件切割上传并带上进度条功能,不妨一试...

    文章来自:脚本之家链接:https://www.jb51.net/article/164316.htm 作者:hhmp 商务合作:请加微信(QQ):2230304070 资源教程分享 码农网-技术教程 ...

  6. Bootstrap组件_进度条

    <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8" ...

  7. Bootstrap中的条纹进度条使用案例

     创建一个条纹的进度条的步骤如下: 1.添加一个带有 class .progress 和 .progress-striped 的 <div> 2.接着在上面的 <div> 内, ...

  8. 如何用 javascript 做一个高逼格的进度条

    可能你发现了本站顶部的进度条,它是如何实现的呢?下面一起来看. 页面进度条展示的是资源下载的进度,通常在页面上加上进度条,可以缓解用户的等待焦虑,也提升了网站的逼格. 前端进度条实现 在前端,实现网页 ...

  9. js 文件上传进度条

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"><title>X ...

最新文章

  1. Dos攻击工具(ZAmbIE)
  2. R语言使用hexSticker包将ggplot2包可视化的结果转换为六角图(六角贴、六角形贴纸、ggplot2 plot to hex sticker)、并自定义设置文本的内容、文本对应的字体
  3. Shell(6)——array的删改unset
  4. Web---JSP-EL表达式
  5. 虚拟化之vmware-vsphere (web) client
  6. [深度学习-原理]BP神经网络
  7. tigerVNC的简单使用教程(CentOS 自带VNC包的远程桌面连接)
  8. 学了阿里P8级架构师的7+1+1落地项目,转疯了!
  9. java dao修改语句_一个通用的DAO模型实现增删改查
  10. Eclipse启动时报错Java was started but returned exit code=13
  11. c语言.h和.c连接错误,conio.h链接错误问题
  12. 时间序列-BP神经网络及与auo arima的比较
  13. JS实现类似QQ好友头像hover时显示资料卡的效果
  14. 重新审视SqlDataReader的使用
  15. 外贸常用术语_最常用的外贸术语大全,收藏备用
  16. 如何Ping特定端口号
  17. Part2 Movielens介绍
  18. 2021年中国工业机器视觉行业产业链分析:人工智能时代带动行业发展[图]
  19. 用户标签照妖镜api、旺旺搜索搜索标签api、用户标签查询api、淘宝用户标签透视透视api
  20. 商城APP软件开发要素有哪些

热门文章

  1. 懂AI值百万年薪?你不知道的开发者薪资榜单大盘点
  2. 简单的文本设计就能影响游戏体验?游戏中提升玩家体验的小设计
  3. 图形渲染技术分享:《GTA V 》图形分析摘要
  4. 技能模块的防外挂机制和同步机制优化
  5. 以CSGO为例 分析不同网络延时下FPS游戏同步的实现
  6. 创造开放世界——《看火人》游戏场景设计
  7. mongodb 对内存的严重占用以及解决方法
  8. vivox60pro和iqoo7pro哪个好
  9. [译文]C# Heap(ing) Vs Stack(ing) in .NET: Part I
  10. Python3.6全栈开发实例[006]