iOS開發(fā)OC -- WKWebView的使用

項(xiàng)目中有的時(shí)候會用加載網(wǎng)頁,簡單記錄一下,后續(xù)有需求會細(xì)化。

@property (nonatomic ,strong) WKWebView * webView;
@property (nonatomic, strong) UIProgressView * progView;

- (void)createView
{
    _webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    _webView.navigationDelegate = self;
    _webView.UIDelegate = self;
    //監(jiān)測進(jìn)度
    [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_model.url]]];
    [self.view addSubview:_webView];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //進(jìn)度條初始化
    _progView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 2)];
    _progView.backgroundColor = [UIColor blueColor];
    //設(shè)置進(jìn)度條的高度,下面這句代碼表示進(jìn)度條的寬度變?yōu)樵瓉淼?倍,高度變?yōu)樵瓉淼?.5倍.
    _progView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
    //沒有進(jìn)度的時(shí)候的顏色
    _progView.trackTintColor = [UIColor whiteColor];
    [self.view addSubview:_progView];
}

//進(jìn)度條監(jiān)控方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        self.progView.progress = self.webView.estimatedProgress;
        if (self.progView.progress == 1) {
            /*
             *添加一個(gè)簡單的動(dòng)畫,將progressView的Height變?yōu)?.4倍,在開始加載網(wǎng)頁的代理中會恢復(fù)為1.5倍
             *動(dòng)畫時(shí)長0.25s,延時(shí)0.3s后開始動(dòng)畫
             *動(dòng)畫結(jié)束后將progressView隱藏
             */
            __weak typeof (self)weakSelf = self;
            [UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
                weakSelf.progView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);
            } completion:^(BOOL finished) {
                weakSelf.progView.hidden = YES;
                
            }];
        }
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

#pragma mark - WKWebView Delegate

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    // 頁面開始加載時(shí)調(diào)用
    [self showProgressDialog];
    //開始加載網(wǎng)頁時(shí)展示出progressView
    self.progView.hidden = NO;
    //開始加載網(wǎng)頁的時(shí)候?qū)rogressView的Height恢復(fù)為1.5倍
    self.progView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
    //防止progressView被網(wǎng)頁擋住
    [self.view bringSubviewToFront:self.progView];
}

- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{
    // 當(dāng)內(nèi)容開始返回時(shí)調(diào)用
    [self hideProgressDialog];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    // 頁面加載完成之后調(diào)用
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation;
{
   // 頁面加載失敗時(shí)調(diào)用
    self.progView.hidden = YES;
}

- (void)dealloc {
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容