iOS中WKWebView仿微信加載進(jìn)度條
本文實(shí)例為大家分享了WKWebView仿微信加載進(jìn)度條的具體代碼,供大家參考,具體內(nèi)容如下
WKWebView添加了estimatedProgress屬性(double類型),我們可以利用該屬性來設(shè)置UIProgressView
為頁面添加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];
頁面開始加載時(shí),隱藏進(jìn)度條
//開始加載
-(void)webView:(WKWebView *)webView
didStartProvisionalNavigation:(WKNavigation *)navigation{
//開始加載的時(shí)候,讓進(jìn)度條顯示
self.progressView.hidden = NO;
}
kvo 監(jiān)聽進(jìn)度
//kvo 監(jiān)聽進(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)聽
-(void)dealloc{
[self.mywebView removeObserver:self
forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iOS WKWebView適配實(shí)戰(zhàn)篇
- iOs遷至WKWebView跨過的一些坑
- iOS開發(fā)教程之WKWebView與JS的交互
- vue 項(xiàng)目 iOS WKWebView 加載
- 簡(jiǎn)單說說iOS之WKWebView的用法小結(jié)
- iOS中WKWebView的一些特殊使用總結(jié)
- iOS使用WKWebView加載HTML5不顯示屏幕寬度的問題解決
- iOS和JS交互教程之WKWebView-協(xié)議攔截詳解
- iOS中wkwebView內(nèi)存泄漏與循環(huán)引用問題詳解
- iOS中WKWebView白屏問題的分析與解決
- 微信小程序iOS下拉白屏晃動(dòng)問題解決方案
- iOS WKWebview 白屏檢測(cè)實(shí)現(xiàn)的示例
相關(guān)文章
Objective-C中編程中一些推薦的書寫規(guī)范小結(jié)
這篇文章主要介紹了Objective-C的一些編程書寫規(guī)范小結(jié),包括類與方法等面向?qū)ο缶幊滔嚓P(guān)的代碼編寫風(fēng)格,需要的朋友可以參考下2016-04-04
iOS開發(fā)項(xiàng)目- 基于WebSocket的聊天通訊(2)
這篇文章主要介紹了iOS開發(fā)項(xiàng)目- 基于WebSocket的聊天通訊,可以實(shí)現(xiàn)錄音和音樂播放,有需要的可以了解一下。2016-11-11
iOS 點(diǎn)擊推送消息跳到應(yīng)用指定頁面的實(shí)例
這篇文章主要介紹了iOS 點(diǎn)擊推送消息跳到應(yīng)用指定頁面的實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-11-11
iOS tableView實(shí)現(xiàn)單選和多選的實(shí)例代碼
本篇文章主要介紹了iOS tableView實(shí)現(xiàn)單選和多選的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
iOS swift 總結(jié)NavigationController出現(xiàn)問題及解決方法
這篇文章主要介紹了iOS swift 總結(jié)NavigationController出現(xiàn)問題及解決方法的相關(guān)資料,需要的朋友可以參考下2016-12-12

