WKWebView 的简单使用

相比UIWebView 的优点 占用内存少 加载速度快

  1. 创建
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.添加加载进度条
该部分来源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];
// 实现监听方法

- (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], 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 后 添加的点击手势会被拦截而无法执行
解决办法(这样可以执行自定义的手势 但是无法屏蔽掉webview 自带的手势 也就是说如果webview 和 self.view 都有添加双击手势的话两者都会被执行)

实现手势的代理方法

// 所在的类遵守协议UIGestureRecognizerDelegate
// 设置添加手势的代理为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 ;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前言 关于UIWebView的介绍,相信看过上文的小伙伴们,已经大概清楚了吧,如果有问题,欢迎提问。 本文是本系列...
    CoderLF阅读 12,988评论 2 12
  • WKWebView-iOS 8出现的,比UIWebView性能高出很多!而且现在xcode也只支持到iOS8.0了...
    芝麻绿豆阅读 5,644评论 0 5
  • 最近公司的项目要加载几个H5页面(不用和js交互)。先前我是用的是Webview,可是在调试的时候发现了问题,加载...
    ZYiDa阅读 3,895评论 0 1
  • 前言: web页面和app的直接的交互是很常见的东西,在ios8之前,用的是uiwebview,但是在ios8之后...
    qingchen91阅读 8,054评论 6 25
  • http://www.cnblogs.com/mddblog/p/5281748.html 一、整体介绍 UIWe...
    F麦子阅读 5,013评论 0 2

友情链接更多精彩内容