一个KVO 实现WKWebView加载进度条的例子 (注意最后移除观察者)
//
// OpenWebViewController.m
// Treasure
//
// Created by 蓝蓝色信子 on 16/7/29.
// Copyright © 2016年 GY. All rights reserved.
// #import "ZTOpenWebViewController.h"
#import <WebKit/WebKit.h> @interface ZTOpenWebViewController ()<WKNavigationDelegate>
//{
// //网页视图
// UIWebView * _webView;
//}
@property (strong, nonatomic) WKWebView *webView; @property (strong, nonatomic) UIProgressView *progressView; @end @implementation ZTOpenWebViewController - (void)viewDidLoad {
[super viewDidLoad];
// //取消导航栏的影响
self.automaticallyAdjustsScrollViewInsets = NO;
self.view.backgroundColor = [UIColor whiteColor]; //[SVProgressHUD show];
//实例化
[self createWebView]; [self creatCustomProgressView];
} -(void)creatCustomProgressView{ //增加加载进度条
// KVO,监听webView属性值得变化(estimatedProgress,title为特定的key)
[_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
[_webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil]; // UIProgressView初始化
self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
self.progressView.frame = CGRectMake(, , _webView.frame.size.width, );
self.progressView.trackTintColor = [UIColor clearColor]; // 设置进度条的色彩
self.progressView.progressTintColor = [UIColor magentaColor];
// 设置初始的进度,防止用户进来就懵逼了(微信大概也是一开始设置的10%的默认值)
[self.progressView setProgress:0.1 animated:YES];
[_webView addSubview:self.progressView];
} - (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:NO];
[super viewWillAppear:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
[SVProgressHUD dismiss];
[super viewWillDisappear:animated];
} - (void)createWebView
{
_webView = [[WKWebView alloc]initWithFrame:self.view.bounds]; [self.view addSubview:_webView]; //调整适应比例
//_webView.scalesPageToFit = YES;
//设置代理
_webView.navigationDelegate = self;
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]]; }
#pragma mark - WKWebView NavigationDelegate //WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
//NSLog(@"是否允许这个导航");
decisionHandler(WKNavigationActionPolicyAllow);
} - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
// Decides whether to allow or cancel a navigation after its response is known. //NSLog(@"知道返回内容之后,是否允许加载,允许加载");
decisionHandler(WKNavigationResponsePolicyAllow);
}
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
//NSLog(@"开始加载");
//self.progress.alpha = 1; //[SVProgressHUD show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; } - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
//NSLog(@"跳转到其他的服务器"); } - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
//NSLog(@"网页由于某些原因加载失败");
//[SVProgressHUD dismiss];
//self.progress.alpha = 0;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation {
//NSLog(@"网页开始接收网页内容");
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
//NSLog(@"网页导航加载完毕");
//[SVProgressHUD dismiss];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; self.title = webView.title;
[webView evaluateJavaScript:@"document.title" completionHandler:^(id _Nullable ss, NSError * _Nullable error) {
//NSLog(@"----document.title:%@---webView title:%@",ss,webView.title);
}];
//self.progress.alpha = 0; } - (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
//NSLog(@"加载失败,失败原因:%@",[error description]);
//self.progress.alpha = 0;
}
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
//NSLog(@"网页加载内容进程终止");
} #pragma mark - KVO监听
// 第三部:完成监听方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([object isEqual:_webView] && [keyPath isEqualToString:@"estimatedProgress"]) { // 进度条 CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
//NSLog(@"打印测试进度值:%f", newprogress); if (newprogress == ) { // 加载完成
// 首先加载到头
[self.progressView setProgress:newprogress animated:YES];
// 之后0.3秒延迟隐藏
__weak typeof(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ weakSelf.progressView.hidden = YES;
[weakSelf.progressView setProgress: animated:NO];
}); } else { // 加载中
self.progressView.hidden = NO;
[self.progressView setProgress:newprogress animated:YES];
}
} else if ([object isEqual:_webView] && [keyPath isEqualToString:@"title"]) { // 标题 //self.title = _webView.title;
} else { // 其他 [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
} -(void)dealloc{
NSLog(@"webVC dealloc = %@",self);
[_webView removeObserver:self forKeyPath:@"estimatedProgress"];
[_webView removeObserver:self forKeyPath:@"title"];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end
一个KVO 实现WKWebView加载进度条的例子 (注意最后移除观察者)的更多相关文章
- iOS WKWebView 加载进度条、导航栏返回&关闭 (Swift 4)
导航: 1.加载进度条 2.导航栏增加返回.关闭按钮 加载进度条 效果图 代码如下: self.progressView.trackTintColor = UIColor.white self.pro ...
- iOS UIWebView 加载进度条的使用-WKWebView的使用,更新2017.6.26
1.由于项目中加载网络插件,直接使用了webview加载.使用了三方NJKWebViewProgress进度条的使用,近期在测试时发现,网络缓慢时出现白屏,有卡顿现象. 于是采用了WKWebView进 ...
- css3 linear-gradient实现页面加载进度条效果
最终效果图: html结构: <div> <p class="p1"> <span></span> < ...
- pace.js – 加载进度条插件
这儿只是简单介绍一下这个插件pace.js. 在页面中引入Pace.js,页面就会自动监测你的请求(包括Ajax请求),在事件循环滞后,会在页面记录加载的状态以及进度情况.此插件的兼容性很好,可以兼容 ...
- 仿UC浏览器图片加载进度条
前几天用UC浏览器看新闻(无意中给UC打了广告),看到它的图片加载进度条,正好最近有时间,所以就自己写了一个. 效果图如下 进度条的底色和填充颜色都可以调整. 首先中间的笑脸作为一个整体,其实现代码如 ...
- 【Web前沿技术】纯 CSS3 打造的10个精美加载进度条动画
之前向大家介绍8款优秀的 jQuery 加载动画和进度条插件,今天这篇文章向大家推荐10个纯 CSS3 代码实现精美加载进度条动画效果的方案.加载动画和进度条在网站和 Web 应用中的使用非常流行,特 ...
- jQuery模拟页面加载进度条
因为我们无法通过任何方法获取整个页面的大小和当前加载了多少,所以想制作一个加载进度条的唯一办法就是模拟.那要怎么模拟呢? 我们知道,页面是从上往下执行的,也就是说我们可以大致估算出在页面的某个位置加载 ...
- 混合开发(一)——WebView开发高级技巧之加载网页以及JavaScript,加载进度条
混合开发(一)--WebView开发高级技巧之加载网页以及JavaScript,加载进度条 现在关于混合开发也越来越多了,很多人喜欢跟随,比如HB,比如RN,其实这东西很早就有这么一个概念了,而且说实 ...
- Unity3D 场景切换加载进度条实现
需要三个场景,场景A,场景B,场景C: 场景A:一个按钮,点击加载场景B: 场景B:从A切换到C过度场景,加载进度条: 场景C:目标场景: 创建OnProgress.cs脚本: using Syste ...
随机推荐
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第1节 Scanner类_3-Scanner的使用步骤
Scanner如何进行键盘输入,引用类型就包含了Scanner,它就是引用类型,所以也有这三个步骤, 导包.创建.使用 先通过api文档找到它.左边输入要查找scanner.双夹scanner右边就会 ...
- 数组的includes方法
Array.prototype.includes方法返回一个布尔值,表示某个数组是否包含给定的值,与字符串的includes方法类似.该方法属于 ES7 ,但 Babel 转码器已经支持. [1, 2 ...
- [Python3 填坑] 006 “杠零”,空字符的使用
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 \0 是空字符,输出时看不到它,但它占 1 个字符的长度 2.2 \0 "遇八进制失效" 2.3 \0 与 '' 不 ...
- JavaBean简介和要求
JavaBean是一种Java语言写成的可重用组件. 所谓javaBean,是指符合如下标准的Java类: 类是公共的 有一个无参的公共的构造器 有属性,且有对应的get.set方法 用户可以使用Ja ...
- Java中的容器(集合)之ArrayList源码解析
1.ArrayList源码解析 源码解析: 如下源码来自JDK8(如需查看ArrayList扩容源码解析请跳转至<Java中的容器(集合)>第十条):. package java.util ...
- 错排问题 && 洛谷 P1595 信封问题
传送门 一道裸的错排问题 错排问题 百度百科上这样说 就是对于一个排列,每一个数都不在正确的位置上的方案数.n 个元素的错排数记为 D(n). 公式 D(n)=(n−1)∗(D(n−2)+D(n−1) ...
- expdp和impdp的应用-高版本通过dblink导入到低版本
今天接到要进行数据库用户的部分数据迁移需求,需求如下 IMP.WO.INSA开头的表只要结构,不要数据 B.TEMP.TMP开头的表不用导 其他表需要导出数据和表结构,同时要求导出此用户下的所有其他对 ...
- Python自学第二天学习之《元组与字典》
一. 元组:tuple类型,元组一级元素 不能修改 不能增加 不能删除,是有序的. 格式 :tu=(1,2,3,4,5,6) 1.类型转换: #字符串转换成元组 b=“123” c=tuple(b) ...
- Neo4j 修改关系类型 type
没有直接修改的函数,也不需要,下面代码就可以: MATCH (n:User {name:"foo"})-[r:REL]->(m:User {name:"bar&qu ...
- vue路由定义
router 根据URL分配到对应的处理程序 单应用页面,vue开发中只有一个一面 例如我们在开发移动端的时候,正常情况下底部的tab有四个选项: 首页 home 发现 find 订 ...