国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS中WKWebView仿微信加載進度條

2020-07-26 02:20:00
字體:
來源:轉載
供稿:網友

本文實例為大家分享了WKWebView仿微信加載進度條的具體代碼,供大家參考,具體內容如下

WKWebView添加了estimatedProgress屬性(double類型),我們可以利用該屬性來設置UIProgressView

github代碼倉庫上存放的Demo

為頁面添加UIProgressView屬性

@property (nonatomic, strong) WKWebView *mywebView;@property (nonatomic, strong) UIProgressView *progressView;//設置加載進度條

懶加載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時(我是在懶加載時) kvo 添加監控

 [_mywebView addObserver:self      forKeyPath:NSStringFromSelector(@selector(estimatedProgress))      options:0      context:nil];

頁面開始加載時,隱藏進度條

//開始加載-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{ //開始加載的時候,讓進度條顯示 self.progressView.hidden = NO;}

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.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方法里移除監聽

-(void)dealloc{ [self.mywebView removeObserver:self      forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 铜山县| 白水县| 九江市| 安龙县| 新宁县| 阿拉善盟| 龙岩市| 开封县| 河北省| 皋兰县| 密山市| 军事| 万州区| 繁昌县| 始兴县| 巍山| 新竹市| 长沙县| 仁怀市| 陕西省| 宝丰县| 永兴县| 宣恩县| 修文县| 遵义市| 虞城县| 虹口区| 张家港市| 莱州市| 喜德县| 徐汇区| 株洲县| 黑山县| 新野县| 禄丰县| 上虞市| 太和县| 新河县| 贵州省| 同仁县| 章丘市|