1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight - navigationHeight ), configuration: WKWebViewConfiguration())
lazy private var progressView: UIProgressView = { let progress = UIProgressView.init(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: UIScreen.main.bounds.width, height: 2)) progress.tintColor = UIColor.green progress.trackTintColor = UIColor.white return progress }()
override func viewDidLoad() {
webView.addObserver(self, forKeyPath: "title", options: .new, context: nil) webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "estimatedProgress"{ progressView.alpha = 1.0 progressView.setProgress(Float(webView.estimatedProgress), animated: true) if webView.estimatedProgress >= 1.0 { UIView.animate(withDuration: 0.3, delay: 0.1, options: .curveEaseOut, animations: { self.progressView.alpha = 0 }, completion: { (finish) in self.progressView.setProgress(0.0, animated: false) }) } }
else if keyPath == "title" { self.title = self.webView.title } }
|