美文网首页
iOS开发WKWebView监听网页加载进度

iOS开发WKWebView监听网页加载进度

作者: lczalh | 来源:发表于2017-12-24 23:09 被阅读1265次
    //添加观察者模式
        [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    
    #pragma mark - 监听加载进度
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
        
        if ([keyPath isEqualToString:@"estimatedProgress"]) {
            
            if (object ==self.webView) {
                [_progressView setAlpha:1.0f];
                [_progressView setProgress:self.webView.estimatedProgress animated:YES];
                
                if(self.webView.estimatedProgress >=1.0f) {
                    
                    [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
                        [_progressView setAlpha:0.0f];
                    } completion:^(BOOL finished) {
                        [_progressView setProgress:0.0f animated:NO];
                    }];
                    
                }
            }
            else
            {
                [super observeValueForKeyPath:keyPath ofObject:object  change:change context:context];
            }
            
        }
    }
    
    - (void)dealloc{
        [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    }
    
    

    相关文章

      网友评论

          本文标题:iOS开发WKWebView监听网页加载进度

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