美文网首页
WKWebView 的简单使用

WKWebView 的简单使用

作者: codeing小牛 | 来源:发表于2017-01-11 10:45 被阅读173次

    相比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 ;
    }
    

    相关文章

      网友评论

          本文标题:WKWebView 的简单使用

          本文链接:https://www.haomeiwen.com/subject/ecakbttx.html