溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

iOS中怎么利用WKWebView實現(xiàn)微信加載進度條

發(fā)布時間:2021-06-16 14:22:31 來源:億速云 閱讀:337 作者:Leah 欄目:移動開發(fā)

今天就跟大家聊聊有關iOS中怎么利用WKWebView實現(xiàn)微信加載進度條,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據(jù)這篇文章可以有所收獲。

為頁面添加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 添加監(jiān)控

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

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

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

kvo 監(jiān)聽進度

//kvo 監(jiā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))];
}

看完上述內容,你們對iOS中怎么利用WKWebView實現(xiàn)微信加載進度條有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI