相比UIWebView 的優(yōu)點(diǎn) 占用內(nèi)存少 加載速度快
- 創(chuàng)建
WKWebView *webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0,Screen_Width, Screen_Height)];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 取消掉回彈效果
webView.scrollView.bounces = NO ;
[webView loadRequest:request];
[self.view addSubview:webView];
2.添加加載進(jìn)度條
該部分來(lái)源http://blog.csdn.net/hdfqq188816190/article/details/51382388
UIView *progress = [[UIView alloc]initWithFrame:CGRectMake(0, 23, CGRectGetWidth(self.view.frame), 2)];
progress.backgroundColor = [UIColor clearColor];
[self.view addSubview:progress];
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0, 0, 0, 3);
layer.backgroundColor = Theme_Blue.CGColor;
[progress.layer addSublayer:layer];
self.progresslayer = layer;
// 給wkwebView 添加觀察者
[webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
// 實(shí)現(xiàn)監(jiān)聽(tīng)方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
if ([keyPath isEqualToString:@"estimatedProgress"]) {
self.progresslayer.opacity = 1;
//不要讓進(jìn)度條倒著走...有時(shí)候goback會(huì)出現(xiàn)這種情況
if ([change[@"new"] floatValue] < [change[@"old"] floatValue]) {
return;
}
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)dealloc{
[(WKWebView *)self.webview removeObserver:self forKeyPath:@"estimatedProgress"];
}
3.self.view 上添加webview 后 添加的點(diǎn)擊手勢(shì)會(huì)被攔截而無(wú)法執(zhí)行
解決辦法(這樣可以執(zhí)行自定義的手勢(shì) 但是無(wú)法屏蔽掉webview 自帶的手勢(shì) 也就是說(shuō)如果webview 和 self.view 都有添加雙擊手勢(shì)的話兩者都會(huì)被執(zhí)行)
實(shí)現(xiàn)手勢(shì)的代理方法
// 所在的類遵守協(xié)議UIGestureRecognizerDelegate
// 設(shè)置添加手勢(shì)的代理為self
@interface UIViewController ()<UIGestureRecognizerDelegate>
-(void)addTapGestureRecoginize{
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bactToListView)];
doubleTap.delegate = self ;
[doubleTap setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:doubleTap];
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES ;
}