国产探花免费观看_亚洲丰满少妇自慰呻吟_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))];}

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扎兰屯市| 册亨县| 长海县| 沁源县| 西吉县| 竹山县| 罗山县| 江北区| 马山县| 黑龙江省| 凤翔县| 沧源| 内江市| 抚松县| 湘潭县| 金塔县| 新闻| 五家渠市| 高邮市| 嘉鱼县| 凤山市| 潞城市| 承德市| 六枝特区| 阿图什市| 灵武市| 鄢陵县| 盐山县| 信丰县| 措美县| 杭州市| 南充市| 青神县| 连城县| 舟曲县| 聂拉木县| 金坛市| 阜平县| 长汀县| 洞口县| 亳州市|