這篇文章主要介紹了iOS中如何使用WKWebView仿微信加載進(jìn)度條,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體內(nèi)容如下
WKWebView添加了estimatedProgress屬性(double類型),我們可以利用該屬性來(lái)設(shè)置UIProgressView
github代碼倉(cāng)庫(kù)上存放的Demo
為頁(yè)面添加UIProgressView屬性
@property (nonatomic, strong) WKWebView *mywebView;@property (nonatomic, strong) UIProgressView *progressView;//設(shè)置加載進(jìn)度條
懶加載UIProgressView
-(UIProgressView *)progressView{ if (!_progressView) { _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; _progressView.frame = CGRectMake(0, 64, 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]; } return _progressView;}
在初始化WKWebView時(shí)(我是在懶加載時(shí)) kvo 添加監(jiān)控
[_mywebView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:nil];
頁(yè)面開(kāi)始加載時(shí),隱藏進(jìn)度條
//開(kāi)始加載-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{ //開(kāi)始加載的時(shí)候,讓進(jìn)度條顯示 self.progressView.hidden = NO;}
kvo 監(jiān)聽(tīng)進(jìn)度
//kvo 監(jiān)聽(tīng)進(jìn)度-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{ if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.mywebView) { [self.progressView setAlpha:1.0f]; BOOL animated = self.mywebView.estimatedProgress > self.progressView.progress; [self.progressView setProgress:self.mywebView.estimatedProgress animated:animated]; if (self.mywebView.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]; }}
在dealloc方法里移除監(jiān)聽(tīng)
-(void)dealloc{ [self.mywebView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];}
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“iOS中如何使用WKWebView仿微信加載進(jìn)度條”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
網(wǎng)站欄目:iOS中如何使用WKWebView仿微信加載進(jìn)度條-創(chuàng)新互聯(lián)
分享鏈接:http://m.rwnh.cn/article38/ggjsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、搜索引擎優(yōu)化、網(wǎng)站建設(shè)、服務(wù)器托管、云服務(wù)器、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容