iOS 学习 --- OC与JS交互(给WKWebView添加顶部加载进度条)

  1. 在webview顶部添加一个进度条UIProgressView。
  2. 给webVIew添加一个监听属性“estimatedProgress”。
  3. 在监听事件中,设置ProgressView 的进度等于webview的estimatedProgress。

添加WKWebView,并给WKWebView添加监听事件 

- (void)addWKWebView{
    // 创建WKWebView
    if (@available(iOS 8.0, *)) {
        self.wkWebView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    } else {
        // Fallback on earlier versions
    }
    _wkWebView.backgroundColor = [UIColor whiteColor];
    _wkWebView.navigationDelegate = self;
    
    // 设置访问的URL
    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
    // 根据URL创建请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // WKWebView加载请求
    [_wkWebView loadRequest:request];
    // 将WKWebView添加到视图
    [self.view addSubview:_wkWebView];
    //给WKWebView添加监听
    [_wkWebView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:nil];
}

给WKWebView添加ProgressView 

- (void)addProgressView{
    self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    _progressView.frame = CGRectMake(0, 44, SCREEN_WIDTH, 5);
    
    [_progressView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];
    _progressView.progressTintColor = [UIColor greenColor];
    [self.view addSubview:_progressView];
    
}

 WKNavigationDelegate 代理方法

#pragma mark - WKNavigationDelegate
/**
 开始加载
 
 @param webView webView description
 @param navigation navigation description
 */
-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation API_AVAILABLE(ios(8.0)){
    NSLog(@"页面开始加载时调用");
    //开始加载的时候,让进度条显示
    self.progressView.hidden = NO;
}

-(void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation API_AVAILABLE(ios(8.0)){
    NSLog(@"didCommitNavigation");
}

-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation API_AVAILABLE(ios(8.0)){
    NSLog(@"didFinishNavigation");
}

监听方法实现 

#pragma mark - KVO
//kvo 监听进度

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))]
        && object == self.wkWebView) {
        [self.progressView setAlpha:1.0f];
        BOOL animated = self.wkWebView.estimatedProgress > self.progressView.progress;
        [self.progressView setProgress:self.wkWebView.estimatedProgress
                              animated:animated];
        
        if (self.wkWebView.estimatedProgress >= 1.0f) {
            
            [UIView animateWithDuration:0.3f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
                [self.progressView setAlpha:0.0f];
            } completion:^(BOOL finished) {
                [self.progressView setProgress:0.0f animated:NO];
            }];
        }
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

移除监听事件 

-(void)dealloc {
    [self.wkWebView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
    self.wkWebView.navigationDelegate = nil;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值