iOS WKWebView添加进度条02
2017-03-06 13:42
411 查看
之前写了一个是关于webview添加进度条的,现在补一个WKWebView进度条。
//添加一个全局属性
@property(nonatomic,strong)CALayer *progresslayer;
//viewDidLoad里添加监听
[_webViewaddObserver:selfforKeyPath:@"estimatedProgress"options:NSKeyValueObservingOptionNewcontext:nil];
//进度条
UIView *progress = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 3)];
progress.backgroundColor = [UIColor clearColor];
[self.view addSubview:progress];
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0, 0, 0, 3);
layer.backgroundColor = [UIColor greenColor].CGColor;
[progress.layer addSublayer:layer];
self.progresslayer = layer;
- (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;
}
self.progresslayer.frame = CGRectMake(0, 0, self.view.bounds.size.width * [change[@"new"] floatValue], 3);
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, 3);
});
}
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
//移除监听
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[_webView removeObserver:self forKeyPath:@"estimatedProgress"];
}
//添加一个全局属性
@property(nonatomic,strong)CALayer *progresslayer;
//viewDidLoad里添加监听
[_webViewaddObserver:selfforKeyPath:@"estimatedProgress"options:NSKeyValueObservingOptionNewcontext:nil];
//进度条
UIView *progress = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 3)];
progress.backgroundColor = [UIColor clearColor];
[self.view addSubview:progress];
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0, 0, 0, 3);
layer.backgroundColor = [UIColor greenColor].CGColor;
[progress.layer addSublayer:layer];
self.progresslayer = layer;
- (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;
}
self.progresslayer.frame = CGRectMake(0, 0, self.view.bounds.size.width * [change[@"new"] floatValue], 3);
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, 3);
});
}
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
//移除监听
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[_webView removeObserver:self forKeyPath:@"estimatedProgress"];
}
相关文章推荐
- iOS WKWebView添加网页加载进度条(转)
- iOS WKWebView添加网页加载进度条
- IOS WKWebView初级使用 以及WKWebView进度条的添加
- iOS实现微信网页进度条,WKWebview进度条的实现
- 浅谈iOS中的WKWebView添加cookie
- iOS WKWebView 加载进度条、导航栏返回&关闭 (Swift 4)
- iOS WKWebView 获得title和加载进度
- iOS WKWebView中显示进度条及HTML的title
- iOS 添加WKWebView导致控制器无法释放的问题
- iOS webview,WKWebView长按弹出框UIMenuController添加自定义功能
- ios-webview加载进度中执行多次的问题
- iOS 8 WKWebView
- iOS 8 WkWebView 网页的配置和前进,后退,js 交互和进度条的加载
- IOS网络第七天WebView-02WebView和网页的交互2,删除大众点评多余文字,加上蒙版进度
- Android WebView 加载网页添加进度条
- iOS 使用NJKWebViewProgress做webview进度条
- 【iOS】WKWebView
- Android webview如何上传文件和添加进度条
- IOS 使用NJKWebViewProgress做webview加载进度条
- iOS 使用NJKWebViewProgress做webview进度条