ios開發(fā)加載webview顯示進(jìn)度條實(shí)例
很多APP加載webView頁面的時(shí)候都有進(jìn)度條顯示,今天我們這里主要使用相對輕量級的WKWebView加載網(wǎng)頁,至于WKWebView 和UIWebView的區(qū)別與聯(lián)系這里就不多講了,自己百度哈哈。。。
WKWebView加載網(wǎng)頁進(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)度條初始化的時(shí)候會(huì)給一定的長度顯示(原因下面解釋)。
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)度條的時(shí)候之所以給一定的默認(rèn)長度主要是因?yàn)樵跊]有網(wǎng)絡(luò)的狀態(tài)下會(huì)立即進(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("點(diǎn)擊重新加載", for: .normal)
button?.addTarget(self, action: #selector(loadURL), for: .touchUpInside)
view.addSubview(button!)
return
}
btn.isHidden = false
}
}
5、記載失敗后點(diǎn)擊提示重新加載
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)聽,離開頁面的時(shí)候需要移除KVO監(jiān)聽,否則會(huì)出現(xiàn)內(nèi)存泄露
deinit {
webView!.removeObserver(self, forKeyPath: "estimatedProgress")
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能(二)
這篇文章主要介紹了IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能,點(diǎn)擊獲取驗(yàn)證碼,進(jìn)入時(shí)間倒計(jì)時(shí),感興趣的小伙伴們可以參考一下2016-04-04
iOS使用xib手動(dòng)實(shí)現(xiàn)動(dòng)畫效果的方法
下面小編就為大家分享一篇iOS使用xib手動(dòng)實(shí)現(xiàn)動(dòng)畫效果的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
iOS快速實(shí)現(xiàn)環(huán)形漸變進(jìn)度條
之前看到很多環(huán)形進(jìn)度條,看上去很酷,然后就試著學(xué)習(xí)他們的代碼,結(jié)果發(fā)現(xiàn)實(shí)現(xiàn)一個(gè)環(huán)形進(jìn)度條一點(diǎn)也不簡單。我就在想一個(gè)簡單的進(jìn)度條有這么復(fù)雜嗎?自己摸索后,有一個(gè)簡單的實(shí)現(xiàn)方法?,F(xiàn)在分享給大家,有需要的朋友們可以參考借鑒。2016-10-10
iOS開發(fā)中音頻視頻播放的簡單實(shí)現(xiàn)方法
視頻音頻是我們在ios日常開發(fā)中經(jīng)常會(huì)遇到的一個(gè)需求,所以下面這篇文章主要給大家介紹了關(guān)于iOS開發(fā)中音頻視頻播放的簡單實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
iOS開發(fā)之UITableView左滑刪除等自定義功能
今天來給大家介紹下iOS開發(fā)中UITableView左滑實(shí)現(xiàn)微信中置頂,刪除等功能。對大家開發(fā)iOS具有一定的參考借鑒價(jià)值,有需要的朋友們一起來看看吧。2016-09-09

