iOS WKWebView带进度条封装(只用传入url,可改变进度条颜色)
1 NSTimer+addition.h
#import <Foundation/Foundation.h>
@interface NSTimer (addition)
/** 暂停时间 */
- (void)w_pauseTime;
/** 获取内容所在当前时间 */
- (void)w_webPageTime;
/** 当前时间 time 秒后的时间 */
- (void)w_webPageTimeWithTimeInterval:(NSTimeInterval)time;
@end
NSTimer+addition.m
#import "NSTimer+addition.h"
@implementation NSTimer (addition)
- (void)w_pauseTime{
//判断定时器是否有效
if (!self.isValid) {
return;
}
//停止计时器
[self setFireDate:[NSDate distantFuture]];
}
- (void)w_webPageTime{
//判断定时器是否有效
if (!self.isValid) {
return;
}
//返回当期时间
[self setFireDate:[NSDate date]];
}
- (void)w_webPageTimeWithTimeInterval:(NSTimeInterval)time{
//判断定时器是否有效
if (!self.isValid) {
return;
}
[self setFireDate:[NSDate dateWithTimeIntervalSinceNow:time]];
}
@end
2 WWebProgressLayer.h
#import <QuartzCore/QuartzCore.h>
@interface WWebProgressLayer : CAShapeLayer
/** 开始加载 */
- (void)w_startLoad;
/** 完成加载 */
- (void)w_finishedLoadWithError:(NSError *)error;
/** 关闭时间 */
- (void)w_closeTimer;
- (void)w_WebViewPathChanged:(CGFloat)estimatedProgress;
@end
WWebProgressLayer.m
#import "WWebProgressLayer.h"
#import <UIKit/UIKit.h>
#import "NSTimer+addition.h"
static NSTimeInterval const ProgressTimeInterval = 0.03;
@interface WWebProgressLayer()
@property (nonatomic, strong) CAShapeLayer *layer;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) CGFloat plusWidth;
@end
@implementation WWebProgressLayer
- (instancetype)init {
self = [super init];
if (self) {
[self initBezierPath];
}
return self;
}
- (void)initBezierPath {
//绘制贝塞尔曲线
UIBezierPath *path = [UIBezierPath bezierPath];
//起点
[path moveToPoint:CGPointMake(0 , 3)];
//终点
[path addLineToPoint:CGPointMake(APP_WIDTH , 3)];
self.path = path.CGPath;
self.strokeEnd = 0;
_plusWidth = 0.005;
self.lineWidth = 2;
self.strokeColor = [UIColor redColor].CGColor;
_timer = [NSTimer scheduledTimerWithTimeInterval:ProgressTimeInterval target:self selector:@selector(pathChanged:) userInfo:nil repeats:YES];
[_timer w_pauseTime];
}
// 设置进度条增加的进度
- (void)pathChanged:(NSTimer *)timer{
self.strokeEnd += _plusWidth;
if (self.strokeEnd > 0.60) {
_plusWidth = 0.002;
}
if (self.strokeEnd > 0.85) {
_plusWidth = 0.0007;
}
if (self.strokeEnd > 0.93) {
_plusWidth = 0;
}
}
//在KVO 计算 实际的读取进度时,调用改方法
- (void)w_WebViewPathChanged:(CGFloat)estimatedProgress {
self.strokeEnd = estimatedProgress;
}
- (void)w_startLoad {
[_timer w_webPageTimeWithTimeInterval:ProgressTimeInterval];
}
- (void)w_finishedLoadWithError:(NSError *)error {
CGFloat timer;
if (error == nil) {
[self w_closeTimer];
timer = 0.5;
self.strokeEnd = 1.0;
}else {
timer = 45.0;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timer * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (timer == 45.0) {
[self w_closeTimer];
}
self.hidden = YES;
[self removeFromSuperlayer];
});
}
#pragma mark - private
- (void)w_closeTimer {
[_timer invalidate];
_timer = nil;
}
- (void)dealloc {
[self w_closeTimer];
}
@end
3.WWebViewController.h
#import "YQViewController.h"
@interface WWebViewController : YQViewController
/** 相关链接*/
@property (nonatomic, copy) NSString *url;
/** 标题 */
@property (nonatomic, copy) NSString *webTitle;
/** 进度条颜色 */
@property (nonatomic, assign) UIColor *progressColor;
@end
WWebViewController.m
#import "WWebViewController.h"
#import "WWebProgressLayer.h"
#import <WebKit/WebKit.h>
@interface WWebViewController ()<WKNavigationDelegate>
@property (nonatomic, strong)WKWebView *WWebView;
@property (nonatomic, strong)WWebProgressLayer *webProgressLayer;
@end
@implementation WWebViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
self.navigationItem.title = self.webTitle;
[self setUpUI];
}
- (void)setUpUI {
self.WWebView = [[WKWebView alloc] initWithFrame:self.view.bounds];
self.WWebView.backgroundColor = RGB(239, 239, 239);
self.WWebView.navigationDelegate =self;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
[self.WWebView loadRequest:request];
[self.view addSubview:self.WWebView];
self.webProgressLayer = [[WWebProgressLayer alloc] init];
self.webProgressLayer.frame = CGRectMake(0, 42, APP_WIDTH, 2);
self.webProgressLayer.strokeColor = self.progressColor.CGColor;
[self.navigationController.navigationBar.layer addSublayer:self.webProgressLayer];
}
#pragma mark - UIWebViewDelegate
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
[self.webProgressLayer w_startLoad];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[self.webProgressLayer w_finishedLoadWithError:nil];
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
[self.webProgressLayer w_finishedLoadWithError:error];
}
- (void)dealloc {
[self.webProgressLayer w_closeTimer];
[_webProgressLayer removeFromSuperlayer];
_webProgressLayer = nil;
}
@end
4.调用
WWebViewController *WWebVC = [[WWebViewController alloc] init];
WWebVC.url = @"https://hr-ez.com/index/sharelist.html?uid=120";
WWebVC.webTitle = @"详情";
WWebVC.progressColor = [UIColor blueColor];
相关文章:

Ubuntu上配置VS Code调试C++
直接使用GDB在Ubuntu上调试C code,有时不是很方便,这里介绍下在Ubuntu上通过Visual Studio Code调试C code操作步骤,通过CMake编译。 安装所需依赖: (1).在Ubuntu上安装Visual Studio Code最新稳定版本1.51.1; (2).…
因果关系是通向强AI的阶梯or作用被夸大?
整理 | 夕颜出品 | AI科技大本营(ID:rgznai100)一直以来,机器学习和统计学之间的界限就比较模糊,比如诺奖得主托马斯萨金特就曾经说过人工智能其实就是统计学,只不过用了一个很华丽的辞藻。但同时也有人认为࿰…

Android中设置TextView的颜色setTextColor
tv.setTextColor(Color.parseColor("#FFFFFF")); tv.setTextColor(Color.WHITE); tv.setTextColor(Color.rgb(255, 255, 255)); //注意Color是大写C,不是color.holo_orange_dark,这样错误并没效果的 tv.setBackgroundResource(R.drawable.icon_bg_rectan…

iOS 跑马灯封装(带点击事件)
1.WAdvertScrollView.h#import <UIKit/UIKit.h>class WAdvertScrollView;typedef enum : NSUInteger {/// 一行文字滚动样式WAdvertScrollViewStyleNormal,/// 二行文字滚动样式WAdvertScrollViewStyleMore, } WAdvertScrollViewStyle;protocol WAdvertScrollViewDelegat…

日期与unix时间戳之间的转换C++实现
之前在https://blog.csdn.net/fengbingchun/article/details/107023645 中介绍过gmtime和localtime的区别,这里介绍下日期与Unix时间戳之间转换的实现,其中也会用到这两个函数。 Unix时间戳(Unix timestamp):是一种时间表示方式,…
模型训练完才是业务的开始?说说模型监控 | CSDN博文精选
扫码参与CSDN“原力计划”作者 | A字头来源 | 数据札记倌(ID:Data_Groom)“模型训练结束后才是业务真正的开始”简述每次模型训练完成后,并不意味着项目的结束,在训练模型后,我们还需要将其稳定上线,然后部署一套相应的监控体系&a…

后端码农谈前端(CSS篇)第一课:CSS概述
一、从扮演浏览器开始 扮演浏览器是Head First图书中很有意义的一个环节。可作者忘记了告诉我们扮演浏览器的台本。我们从这里开始。 上图是webkit内核渲染html和css的流程图。从该图我们可以知道以下几个关键信息: HTML的解析过程和CSS的解析过程是独立完成的。HTM…
远场语音识别错误率降低30%,百度提基于复数CNN网络的新技术
【12月公开课预告】,入群直接获取报名地址12月11日晚8点直播主题:人工智能消化道病理辅助诊断平台——从方法到落地12月12日晚8点直播:利用容器技术打造AI公司技术中台12月17日晚8点直播主题:可重构计算:能效比、通用性…

深度神经网络中的局部响应归一化LRN简介及实现
Alex、Hinton等人在2012年的NIPS论文《ImageNet Classification with Deep Convolutional Neural Networks》中将LRN应用于深度神经网络中(AlexNet)。论文见:http://www.cs.toronto.edu/~hinton/absps/imagenet.pdf ,截图如下: 公式解释&…

iOS 被拒解析
原因: Your app uses the "prefs:root" non-public URL scheme, which is a private entity. The use of non-public APIs is not permitted on the App Store because it can lead to a poor user experience should these APIs change.Continuing to us…

MSSQL数据库统计所有表的记录数
今天需要筛选出来库中行数不为零的表,于是动手写下了如下存储过程。 CREATE PROCEDURE TableCount AS BEGIN SET NOCOUNT ON DECLARE t1 AS TABLE(id INT IDENTITY,NAME NVARCHAR(50),RowsCount INT) DECLARE indexid AS INT DECLARE maxid AS INT DECLARE count A…

经典网络AlexNet介绍
AlexNet经典网络由Alex Krizhevsky、Hinton等人在2012年提出,发表在NIPS,论文名为《ImageNet Classification with Deep Convolutional Neural Networks》,论文见:http://www.cs.toronto.edu/~hinton/absps/imagenet.pdf …
微软张若非:搜索引擎和广告系统,那些你所不知的AI落地技术
【12月公开课预告】,入群直接获取报名地址12月11日晚8点直播主题:人工智能消化道病理辅助诊断平台——从方法到落地12月12日晚8点直播:利用容器技术打造AI公司技术中台12月17日晚8点直播主题:可重构计算:能效比、通用性…

iOS 之 IQKeyboardManager 解决使用UITableView 界面上移问题
- (void)viewWillAppear:(BOOL)animated {[IQKeyboardManager sharedManager].enable NO;}- (void)viewWillDisappear:(BOOL)animated{[super viewWillDisappear:animated];[IQKeyboardManager sharedManager].enable YES; }

excel增加上一列的数值(日期)
TEXT(D2-1,"m月d日") 有年的话就是 TEXT(D2-1,"yyyy年m月d日") D2就是参照日期转载于:https://www.cnblogs.com/hont/p/4352877.html

iOS 一些基础的方法
iOS button字体居中等的设置 self.replyBtn.contentHorizontalAlignment UIControlContentHorizontalAlignmentCenter; UIControlContentHorizontalAlignmentCenter 0, UIControlContentHorizontalAlignmentLeft 1, UIControlContentHorizontalAlignmentRight 2…

经典网络VGGNet介绍
经典网络VGGNet(其中VGG为Visual Geometry Group)由Karen Simonyan等于2014年提出,论文名为《Very Deep Convolutional Networks for Large-Scale Image Recognition》,论文见:https://arxiv.org/pdf/1409.1556.pdf,网络结构如下图…
70行Go代码打败C
【12月公开课预告】,入群直接获取报名地址12月11日晚8点直播主题:人工智能消化道病理辅助诊断平台——从方法到落地12月12日晚8点直播:利用容器技术打造AI公司技术中台12月17日晚8点直播主题:可重构计算:能效比、通用性…
Android开源框架ImageLoader的完美例子
要使用ImageLoader就要到这里下载jar包: https://github.com/nostra13/Android-Universal-Image-Loader 然后导入项目中去就行了 项目文档结构图: 从界面说起,界面本身是没什么好说的,就是如何在xml当中进行定义罢了 有以下这么多…
“掘金”金融AI落地,英特尔趟出一套通关攻略
有人说,金融业是最大的AI应用场景,但不管怎样,不可否认的事实是金融业已经从数字化走向AI化。某种程度上,AI与金融业有着天然的契合性:其一,金融业本身就是以数据为基本元素的行业,它为AI的模型…

深度神经网络中的Inception模块介绍
深度神经网络(Deep Neural Networks, DNN)或深度卷积网络中的Inception模块是由Google的Christian Szegedy等人提出,包括Inception-v1、Inception-v2、Inception-v3、Inception-v4及Inception-ResNet系列。每个版本均是对其前一个版本的迭代改进。另外,依…

iOS隐藏导航栏的方法
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:YES animated:animated]; }

NEW关键字的三种用法
声明:本文最初是本人从他出转载到51CTO上的一篇文章,但现在记不清最初从出处了,原文作者看到还请原来,现在发表在这里只为学习,本人在51CTO的该文章的地址为:http://kestrelsaga.blog.51cto.com/3015222/75…
论文解读 | 微信看一看实时Look-alike推荐算法
作者丨gongyouliu编辑丨lily来源 | 授权转载自大数据与人工智能(ID:ai-big-data)微信看一看的精选文章推荐(见下面图1)大家应该都用过,微信团队在今年发表了一篇文章来专门介绍精选推荐的算法实现细节(Real-time Attention based Look-alike Model,简称R…

经典网络GoogLeNet介绍
经典网络GoogLeNet由Christian Szegedy等于2014年提出,论文名为《Going deeper with convolutions》,论文见:https://arxiv.org/pdf/1409.4842v1.pdf GoogLeNet网络用到了Inception-v1模块,关于Inception模块的介绍可以参考&…

iOS webview 点击按钮返回上一页面或者返回首页
- (void)floatBtn:(UIButton *)sender { NSLog("点击"); if ([self.webView canGoBack]) { [self.webView goBack]; } else{ [self.view resignFirstResponder]; [self.navigationController popViewControllerAnimate…

centos6.6 Kickstart无人值守安装(一):原理篇
为什么80%的码农都做不了架构师?>>> #为什么要自动化无人值守安装? 偷懒……nb……zb……geekno no no 瞬间完成大规模机器部署,提高生产力,节省时间精力,为公司谋取更多利益,实现社会和谐!#怎…
拿来就能用!如何用 AI 算法提高安全运维效率?
作者 | 黄龙责编 | 伍杏玲来源 | CSDN(ID:CSDNnews) 在整个安全工作中,安全运维是不可或缺的一环,其目的是保证各项安全工作持续有效地运作。除了对外的沟通和业务对接相关工作,大部分安全运维的日常工作相…

深度神经网络中Inception-ResNet模块介绍
之前在https://blog.csdn.net/fengbingchun/article/details/113482036 介绍了Inception,在https://blog.csdn.net/fengbingchun/article/details/114167581 介绍了ResNet,这里介绍下深度神经网络中的Inception-ResNet模块。 介绍Inception-ResNet的论文…

iOS 让UIView的左上角和右上角为圆角
-(UIView *)platFormBGV{ if (!_platFormBGV) { _platFormBGV [[UIView alloc] init]; _platFormBGV.backgroundColor [UIColor whiteColor]; _platFormBGV.frame CGRectMake(0, self.view.frame.size.height, APP_WIDTH, 220); // 左上和右上为圆角 UIBezierPath *cornerRa…