前言

为什么要说 WKWebview,在之前做电子书笔记时已经提过 WKWebview 在iOS8之后已完全替代 Webview,原因就不多说了,主要还是内存过大;

封装

封装一个基于 UIViewController 类: WKWebViewController

WKWebViewController.h

@interface WKWebViewController : UIViewController
///目标URL
@property (nonatomic,strong) NSString *url;
@property (nonatomic,strong) WKWebView *webview;
@end

WKWebViewController.m

#import "WKWebViewController.h"

@interface WKWebViewController ()<WKNavigationDelegate,UIScrollViewDelegate>
///进度条
@property (nonatomic, strong) UIProgressView *progressView;
@end @implementation WKWebViewController - (void)viewDidLoad {
[super viewDidLoad];
///添加 KVO 对进度监听
[self.webview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
[self.view addSubview:self.webview];
[self.view addSubview:self.progressView]; } #pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"estimatedProgress"]) {
self.progressView.progress = self.webview.estimatedProgress;
if (self.progressView.progress == ) {
__weak typeof (self)weakSelf = self;
[UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);
} completion:^(BOOL finished) {
weakSelf.progressView.hidden = YES; }];
}
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
} #pragma mark - webview 代理
//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
NSLog(@"开始加载网页");
//开始加载网页时展示出progressView
self.progressView.hidden = NO;
//开始加载网页的时候将progressView的Height恢复为1.5倍
self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
//防止progressView被网页挡住
[self.view bringSubviewToFront:self.progressView];
}
//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
NSLog(@"加载完成");
//加载完成隐藏progressView
self.progressView.hidden = YES;
}
//加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
NSLog(@"加载失败");
//加载失败隐藏progressView
self.progressView.hidden = YES;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return nil;
}
#pragma mark - 属性
- (WKWebView *)webview{
if (!_webview) {
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
// 自适应屏幕宽度js
NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; // 添加自适应屏幕宽度js调用的方法
[wkWebConfig.userContentController addUserScript:wkUserScript];
_webview = [[WKWebView alloc]initWithFrame:CGRectMake(, , self.view.bounds.size.width, self.view.bounds.size.height - (wkj_IsIphoneX ? + : )) configuration:wkWebConfig];
_webview.navigationDelegate = self;
_webview.scrollView.delegate = self;
_webview.opaque = NO;
}
return _webview;
}
- (UIProgressView *)progressView{
if (!_progressView) {
_progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(, , [[UIScreen mainScreen] bounds].size.width, )];
_progressView.trackTintColor = [ColorKLSystem colorWithAlphaComponent:0.5];
_progressView.tintColor = ColorKLSystem;
_progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
}
return _progressView;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

使用

新建一个基于WKWebViewController的VC,设置URL,然后加载就可以了

iOS-WKWebview 带有进度条加载的ViewController【KVO监听Webview加载进度】的更多相关文章

  1. js - 预加载+监听图片资源加载制作进度条

    这两天遇到一个新需求:一个一镜到底的h5动画.因为功能的特殊性,就要求我们提前监听页面的静态图片是否全部加载完毕.即处理预加载. 总结下来,下次这种需求需要提前注意以下几点: 一.图片而不是背景图 本 ...

  2. jQuery学习(监听DOM加载)

    jQuery的extend方法 function njQuery() { } /* njQuery.extend = function (obj) { // 此时此刻的this就是njQuery这个类 ...

  3. C# NanUI WinFormium监听页面加载开始\结束

    个人博客 地址:https://www.wenhaofan.com/article/20190501213608 因为NanUI文档中仅介绍了Formium窗口的监听,但是没有WinFormium相关 ...

  4. iOS: 使用KVO监听控制器中数组的变化

    一.介绍: KVO是一种能动态监听到属性值的改变的方式,使用场景非常广泛,这里我只讲如何监听控制器ViewController中数组的变化. 二.了解: 首先我们应该知道KVO是不能直接监听控制器Vi ...

  5. SpringMVC 监听文件上传进度

    Spring MVC 监听文件上传进度 具体实现分三个步骤: 接管CommonsMultipartResolver,重写针对文件上传的请求. 在第一步中写入监听,以获取上传进度. 修改上传部分的配置文 ...

  6. 李洪强iOS开发本人集成环信的经验总结_07_监听好友请求

    李洪强iOS开发本人集成环信的经验总结_07_监听好友请求 来到Appdalegate中: 遵守代理协议 设置代理  实现监听好友请求的回调的方法

  7. 监听spring加载完成后事件

    有这个想法是在很早以前了,那时的我没有接触什么缓存技术,只知道hibernate有个二级缓存.没有用过memcache,也没有使用过redis. 只懂得将数据放到数组里或者集合里,一直不去销毁它(只有 ...

  8. Android中监听webview监听是否加载完成

    之前写过一篇捕获Phoengap的webview事件的方法,主要是在实现了CordovaInterface的Activity中,  在onMessage中根据第一个参数的message name来判断 ...

  9. 【winform】基于UserControl实现webBrower组件时html页面元素加载及onclick事件监听实现

    [背景]基于System.Windows.Forms.UserControl实现的webBrower组件在html内使用window.external调用winform事件失败. [解决思路]借助wi ...

随机推荐

  1. Android界面设计

    从继承关系来看,所有组件继承自View.容器也是继承自View,它能容纳别的View. 所有容器继承自ViewGroup.包括 FrameLayout, LinearLayout, RelativeL ...

  2. windown 安装配置 mvn不是内部或外部命令

    path 检查没有任何问题,就直接把maven路径直接放到path前面 如果提示JAVA_HOME not found 之类错误,环境变量中需要设置JAVA_HOME,而且需要在path中添加%JAV ...

  3. Find one unique integer

    https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...

  4. 2018.09.16 atcoder Garbage Collector(贪心)

    传送门 昨晚打比赛的时候不是很机智啊. 这道题贪心就能过了. 我们可以发现一个明显的结论,每次选的垃圾的距离从大到小排序之后,每个距离对答案的贡献的系数是5,5,7,9,11-也就是最远的是5,其余都 ...

  5. 2018.09.15 poj1734Sightseeing trip(floyd求最小环)

    跟hdu1599差不多.. 只是需要输出方案. 这个可以递归求解. 代码: #include<iostream> #include<cstdio> #include<cs ...

  6. 23. Man and His Natural Habitat 人类及其自然栖息地

    . Man and His Natural Habitat 人类及其自然栖息地 ① Ecology is that branch of science which concerns itself wi ...

  7. MySQL】存储过程、游标、循环简单实例

    create procedure my_procedure() -- 创建存储过程 begin -- 开始存储过程 declare my_id varchar(32); -- 自定义变量1 decla ...

  8. Vivado 常见报错

    1.[Synth 8-2543] port connections cannot be mixed ordered and named 说明例化时最后一个信号添加了一个逗号. 2. 原因:报告说明有一 ...

  9. Python调用Google翻译

    出自:http://blog.csdn.net/zhaoyl03/article/details/8830806 最近想动手做一个文档自动下载器,需要模拟浏览器的行为.虽然感觉思路上没有困难,但在技术 ...

  10. python编码(二)

    谈谈Unicode编码,简要解释UCS.UTF.BMP.BOM等名词 问题一 使用Windows记事本的“另存为”,可以在GBK.Unicode.Unicode big endian和UTF-8这几种 ...