一个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 ...
随机推荐
- 【cs231n作业笔记】一:KNN分类器
安装anaconda,下载assignment作业代码 作业代码数据集等2018版基于python3.6 下载提取码4put 本课程内容参考: cs231n官方笔记地址 贺完结!CS231n官方笔记授 ...
- 2019 Nanchang Onsite
D.Interesting Series F(n)实际上是一个等比数列的和,将它从递推式转变为通项公式(a^n-1)/(a-1),这里只需要确定n就可以. 题目要求选取k大小的所有子集的答案求和,可以 ...
- 007-Spring Boot-@Enable*注解的工作原理-EnableConfigurationProperties、ImportSelector、ImportBeanDefinitionRegistrar
一.@Enable* 启用某个特性的注解 1.EnableConfigurationProperties 回顾属性装配 application.properties中添加 tomcat.host=19 ...
- Python Error: “ImportError: No module named six”,用自动安装解决依赖问题
在初次运行带有matplotlib包的程序时,被告知了缺少模块(如标题所示).搜索调查后发现在自己安装的python中确实缺少此安装包,接下来,进行了下载.安装.运行,又少了一个模块,再下载.再运行, ...
- 【MM系列】SAP MM模块-组织结构介绍
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-组织结构介绍 ...
- YOLOv3 算法的详细说明
YOLOv3没有太多的创新,主要是借鉴一些好的方案融合到YOLO里面.不过效果还是不错的,在保持速度优势的前提下,提升了预测精度,尤其是加强了对小物体的识别能力. 本文主要讲v3的改进,由于是以v1和 ...
- C语言第七周作业
每个单词的最后一个字母改成大写 函数fun的功能是:将p所指字符串中每个单词的最后一个字母改成大写.(这里的"单词"是指由空格隔开的字符串). 函数接口定义: void fun( ...
- linux shell 中的数组的取值 遍历 替换 删除操作
引言 在Linux平台上工作,我们经常需要使用shell来编写一些有用.有意义的脚本程序.有时,会经常使用shell数组.那么,shell中的数组是怎么表现的呢,又是怎么定义的呢?接下来逐一的进行讲解 ...
- spring源码下载及转入ECLIPSE
转自:https://www.cnblogs.com/scevecn/p/6043284.html 本例spring源码版本是4.3.0的, 所以jdk需要准备1.8的(不同版本源码要求的jdk不一样 ...
- locale报错,显示中文乱码
locale: Cannot set LC_CTYPE to default locale: No such file or directorylocale: Cannot set LC_MESSAG ...