#import "informationDetailViewController.h"
#import <WebKit/WebKit.h>
@interface informationDetailViewController ()<WKNavigationDelegate>
@property (nonatomic, strong) WKWebView * webView;
@property(nonatomic)CALayer *progresslayer;
@end
@implementation informationDetailViewController
- (void)viewDidLoad {
self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
self.webView.scrollView.bounces = NO;
self.webView.navigationDelegate = self;
self.webView.backgroundColor = Default_BackgroudColor;
//给webView添加KVO监听
[self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
[self.view addSubview:self.webView];
//创建进度条
UIView * progress = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 2)];
progress.backgroundColor = [UIColor clearColor];
[self.view addSubview:progress];
//给进度条添加layer层
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0, 0, 0, 1);
layer.backgroundColor = color_lan_se_an_niu.CGColor;
[progress.layer addSublayer:layer];
self.progresslayer = layer;
}
- (void)viewWillAppear:(BOOL)animated {
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
if ([keyPath isEqualToString:@"estimatedProgress"]) {
self.progresslayer.opacity = 1;
//不要让进度条倒着走...有时候goback会出现这种情况
if ([change[@"new"] floatValue] < [change[@"old"] floatValue]) {
return;
}
NSLog(@"progress%f",[change[@"new"] floatValue] );
self.progresslayer.frame = CGRectMake(0, 0, self.view.bounds.size.width * [change[@"new"] floatValue], 2);
if ([change[@"new"] floatValue] == 1) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.progresslayer.opacity = 0;
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.progresslayer.frame = CGRectMake(0, 0, 0, 2);
});
}
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)viewWillDisappear:(BOOL)animated {
[self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
[self removeObserver:self forKeyPath:@"estimatedProgress"];
}
@end
webView进度条加载
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 使用到的第三方库:NJKWebViewProgress 使用:采用代理方式,在代理中获取进度,并赋值给进度条。具体...
- 首先我们看效果图 1. 启动页部分 基本思路如下: 在app启动的时候 在window对象上添加一个视图,我这里用...
- WebView中浏览图片、显示加载进度、仿微信显示关闭按钮这三个功能在不同的项目中都有写到,今天就给稍微整理一下,...
- 来源:NumPy Tutorial - TutorialsPoint 译者:飞龙 协议:CC BY-NC-SA 4...
- 一、WKWebView 使用WKWebView替换UIWebView可以大量减少运行内存。用法与UIWebView...