1. 思路是给WKWebView添加KVO监听,在KVO的监听中能获取到加载进度,从而实现进度条
下面是源码
#import "BookCityStoryVC.h"
#import <WebKit/WebKit.h>
@interface BookCityStoryVC () <WKNavigationDelegate, WKUIDelegate> {
WKWebView *webview;
}
@property (nonatomic,weak) CALayer *progressLayer;
@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipeGestureRecognizer;
@property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer;
@end
@implementation BookCityStoryVC
- (void)viewDidLoad {
[super viewDidLoad];
[self WebKit];
}
- (void)WebKit {
self.view.backgroundColor = [UIColor whiteColor];
CGFloat view_w = self.view.frame.size.width;
CGFloat view_h = self.view.frame.size.height;
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
config.selectionGranularity = WKSelectionGranularityDynamic;
config.allowsInlineMediaPlayback = YES;
webview = [[WKWebView alloc] initWithFrame:CGRectMake(0, 64, view_w, view_h - 64) configuration:config];
[self.view addSubview:webview];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]];
[webview loadRequest:request];
webview.navigationDelegate = self;
webview.UIDelegate = self;
[webview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
[self setupProgress];
self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
[self.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
}
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
if (webview.canGoBack) {
[webview goBack];
}
}
if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
if (webview.canGoForward) {
[webview goForward];
}
}
}
-(void)setupProgress{
UIView *progress = [[UIView alloc]init];
progress.frame = CGRectMake(0, 64, self.view.frame.size.width, 3);
progress.backgroundColor = [UIColor clearColor];
[self.view addSubview:progress];
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0, 0, 0, 3);
layer.backgroundColor = [UIColor blueColor].CGColor;
[progress.layer addSublayer:layer];
self.progressLayer = layer;
}
#pragma mark - KVO回馈
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if ([keyPath isEqualToString:@"estimatedProgress"]) {
self.progressLayer.opacity = 1;
if ([change[@"new"] floatValue] <[change[@"old"] floatValue]) {
return;
}
self.progressLayer.frame = CGRectMake(0, 0, self.view.frame.size.width*[change[@"new"] floatValue], 3);
if ([change[@"new"]floatValue] == 1.0) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.progressLayer.opacity = 0;
self.progressLayer.frame = CGRectMake(0, 0, 0, 3);
});
}
}
}
//
- (void)dealloc {
[webview removeObserver:self forKeyPath:@"estimatedProgress"];
}