欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS中WKWebView仿微信加載進(jìn)度條

 更新時(shí)間:2019年05月22日 09:38:51   作者:抬頭看見檸檬樹  
這篇文章主要為大家詳細(xì)介紹了iOS中WKWebView仿微信加載進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了WKWebView仿微信加載進(jìn)度條的具體代碼,供大家參考,具體內(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è)面開始加載時(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))];
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論