溫馨提示×

溫馨提示×

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

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

ios如何開發(fā)加載webview顯示進(jìn)度條

發(fā)布時間:2021-06-30 15:11:52 來源:億速云 閱讀:276 作者:小新 欄目:移動開發(fā)

這篇文章給大家分享的是有關(guān)ios如何開發(fā)加載webview顯示進(jìn)度條的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

WKWebView加載網(wǎng)頁進(jìn)度跳顯示主要效果如下:

ios如何開發(fā)加載webview顯示進(jìn)度條

這里主要是使用KVO監(jiān)聽WKWebView的“estimatedProgress”屬性,通過監(jiān)聽該屬性的變化才是進(jìn)度條的長度。

1、定義便利構(gòu)造函數(shù)、以及屬性和控件

var url: String?
  var progresslayer = CALayer()
  var webView: WKWebView?
  var button: UIButton?
convenience init(title: String, url: String) {
    self.init()
    self.title = title
    self.url = url
  }

2、創(chuàng)建webview控件,并監(jiān)聽estimatedProgress,進(jìn)度條初始化的時候會給一定的長度顯示(原因下面解釋)。

func setupUI() {
    webView = WKWebView(frame: CGRect.init(x: 0, y: 0, width: screenWidth, height: screenHeight-64.0))

    if url == "" {
      url = "http:www.baidu.com"
    }
    let request = URLRequest(url: URL(string: url ?? "http:www.baidu.com")!)
    webView?.load(request)
    webView?.uiDelegate = self
    webView?.navigationDelegate = self;
    view.addSubview(webView!)

    //添加屬性監(jiān)聽
    webView?.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
    progresslayer.frame = CGRect.init(x: 0, y: 0, width: screenWidth * 0.1, height: 3)
    progresslayer.backgroundColor = UIColor.green.cgColor
    view.layer.addSublayer(progresslayer)
  }

3、監(jiān)聽estimatedProgress屬性變化,并修改進(jìn)度條長度,創(chuàng)建進(jìn)度條的時候之所以給一定的默認(rèn)長度主要是因為在沒有網(wǎng)絡(luò)的狀態(tài)下會立即進(jìn)度float == 1條件,這樣給人的感覺就是沒有加載網(wǎng)頁一樣。

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "estimatedProgress" {
      progresslayer.opacity = 1
      let float = (change?[NSKeyValueChangeKey.newKey] as! NSNumber).floatValue
      progresslayer.frame = CGRect.init(x: 0, y: 0, width: (screenWidth * CGFloat(float)) , height: 3)
      if float == 1 {
        weak var weakself = self
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2, execute: {
          weakself?.progresslayer.opacity = 0
        })
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.8, execute: {
          weakself?.progresslayer.frame = CGRect.init(x: 0, y: 0, width: 0, height: 3);
        })
      }
    }else{
      super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
    }
  }

4、web view加載失敗后提示

extension KKWebView : WKUIDelegate, WKNavigationDelegate {
  func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
    guard let btn = button else {
      button = UIButton(type: .system)
      button?.frame = CGRect.init(x: 0, y: 3, width: screenWidth, height: screenHeight-64-3)
      button?.backgroundColor = UIColor.white
      button?.setTitleColor(UIColor.darkText, for: .normal)
      button?.setTitle("點擊重新加載", for: .normal)
      button?.addTarget(self, action: #selector(loadURL), for: .touchUpInside)
      view.addSubview(button!)
      return
    }
    btn.isHidden = false
  }
}

5、記載失敗后點擊提示重新加載

 func loadURL() {
    button?.isHidden = true
    if url == "" {
      url = "http:www.baidu.com"
    }
    let request = URLRequest(url: URL(string: url ?? "http:www.baidu.com")!)
    webView?.load(request)
  }

5、移除監(jiān)聽,離開頁面的時候需要移除KVO監(jiān)聽,否則會出現(xiàn)內(nèi)存泄露

deinit {
    webView!.removeObserver(self, forKeyPath: "estimatedProgress")
  }

感謝各位的閱讀!關(guān)于“ios如何開發(fā)加載webview顯示進(jìn)度條”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

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

AI