iOS-WebView(WKWebView)进度条
一直以来,就有想通过技术博客来记录总结下自己工作中碰到的问题的想法,这个想法拖了好久今天才开始着手写自己的第一篇技术博客,由于刚开始写,不免会出现不对的地方,希望各位看到的大牛多多指教。好了,不多说了,直接进入正题~
WKWebView有一个属性estimatedProgress,该属性就是当前网页加载的进度,可以通过KVO来监听这个属性。
WKWebView * webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:webView];
self.webView = webView;
// 添加属性监听
[webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
添加好属性的监听后,接下来就是去实现进度条,这里用到了CALayer隐式动画
// 进度条
UIView * progress = [[UIView alloc] initWithFrame:CGRectMake(, , CGRectGetWidth(self.view.frame), )];
progress.backgroundColor = [UIColor clearColor];
[self.view addSubview:progress]; // 隐式动画
CALayer * layer = [CALayer layer];
layer.frame = CGRectMake(, , , );
layer.backgroundColor = [UIColor blueColor].CGColor;
[progress.layer addSublayer:layer];
self.progressLayer = layer;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]
做好这些基本的工作之后,就是最重要的监听属性的方法了
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if ([keyPath isEqualToString:@"estimatedProgress"]) {
NSLog(@"change == %@",change);
self.progressLayer.opacity = ;
self.progressLayer.frame = CGRectMake(, , self.view.bounds.size.width * [change[NSKeyValueChangeNewKey] floatValue], );
if ([change[NSKeyValueChangeNewKey] floatValue] == ) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(. * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.progressLayer.opacity = ;
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(. * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.progressLayer.frame = CGRectMake(, , , );
});
}
}else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
主要的代码就是这些了,⚠️最后不要忘记取消监听
- (void)dealloc {
[self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}
以上就是给WKWebView添加进度条的方式。最后附上实现的Demo:https://github.com/wanliju/WLWKWebViewWithProgressLine
iOS-WebView(WKWebView)进度条的更多相关文章
- Xamarin iOS教程之进度条和滚动视图
Xamarin iOS教程之进度条和滚动视图 Xamarin iOS 进度条 进度条可以看到每一项任务现在的状态.例如在下载的应用程序中有进度条,用户可以很方便的看到当前程序下载了多少,还剩下多少.Q ...
- WebView线性进度条
参考:http://www.cnblogs.com/hubli/p/4835549.html APP会跳转网页时候,请参考:http://blog.csdn.net/raphael55/article ...
- Android中的webview的进度条
<application android:icon="@drawable/hunqin" android:label="@string/app_name" ...
- webview的进度条的加载,webview的使用以及handle的理解与使用
Webview的几个关键方法要介绍一些: 谷歌官方文档是这么说的; A WebView has several customization points where you can add your ...
- iOS学习-圆形进度条
效果: #import <UIKit/UIKit.h> @interface HsProfitRatePieWidgets : UIView { UILabel *_textLabel; ...
- ios 画圆环进度条
#import <UIKit/UIKit.h> @interface SNCircleProgressView : UIView /** * 进度值0-1.0之间 */ @property ...
- iOS 自定义步骤进度条
新项目要做入驻功能,其中包括一个入住流程,类似登录或者注册流程如下图. 之前想着用自己绘图来做,可是又懒不想多写代码,所以就想着能不能用进度条来做. 1.用进度条做的首先要解决的是进度条的高度问题,可 ...
- iOS圆弧渐变进度条的实现
由于项目需要一个环形渐变进度条显示课程,这方便网上的确有很多相关资料但是,都是比较零散的而且,大多数只是放一堆代码就算完了.这里我想详细写一篇我自己实现这个进度条的过程. 实现一个圆弧进度条主要分为三 ...
- 仿IOS圆形下载进度条
/** * Created by C058 on 2016/5/25. */ public class MyHoriztalProgressBar extends ProgressBar { priv ...
随机推荐
- 软件项目技术点(1)——d3.interpolateZoom-在两个点之间平滑地缩放平移
AxeSlide软件项目梳理 canvas绘图系列知识点整理 软件参考d3的知识点 我们在软件中主要用到d3.js的核心函数d3.interpolateZoom - 在两个点之间平滑地缩放平移.请 ...
- sqlserver查询当月数据
SELECT * FROM table WHERE datediff(month,LoginTime,getdate())=0 ORDER BY LoginTime SELECT * FROM tab ...
- delphi之socket通讯
使用了2个组建: TServerSocket TClientSocket ------------------TServerSocket--------------------------- //开启 ...
- toasf的苦水
http://blog.csdn.net/qq_25867141/article/details/52807705 优化 上面的几种方式我大致也都走了一遍,其实我觉得都没啥区别,看你喜欢用哪种吧.我其 ...
- 关于“为什么不加friend就会提示参数过多”
#include <iostream> using namespace std; class Complex { double real, imag; public: Complex(do ...
- Java接口与多态
接口 可以理解为一种特殊的类,里面全部是由全局常量(static final)和公共的抽象方法所组成 接口的定义格式 接口的数据成员,只允许被public, static, final修饰. 接口的方 ...
- MySQL数据操作(借鉴)
/* 启动MySQL */net start mysql /* 连接与断开服务器 */mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL */mysqld ...
- 这么多小程序,会微信小程序就够了
随着小程序的普及以及小程序体验的逐步升级,现在小程序的地位已经凸现出来.各大平台纷纷推出自己的小程序平台. 最早的是微信小程序,支付宝小程序,快应用,百度小程序去年上来,18年底头条程序也发布. 那么 ...
- 【Leetcode】【Medium】Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 文本处理三剑客之 grep
grep简介 grep(Global search REgular expression and Print out the line)是Linux上的文本处理三剑客之一,另外两个是sed和awk. ...