QSLWebController.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // QSLWebController.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2024/12/24.
  6. //
  7. import UIKit
  8. import WebKit
  9. class QSLWebViewController: QSLBaseController, WKNavigationDelegate {
  10. var isShowTop: Bool = false {
  11. didSet {
  12. updateTop()
  13. }
  14. }
  15. lazy var topView: QSLWebViewTopView = {
  16. let view = QSLWebViewTopView()
  17. return view
  18. }()
  19. var webUrl: String?
  20. var webView: WKWebView = WKWebView()
  21. var progressView:UIProgressView = UIProgressView()
  22. var closeBtn: UIButton?
  23. deinit {
  24. webView.removeObserver(self, forKeyPath:"estimatedProgress")
  25. webView.navigationDelegate = nil
  26. }
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. webView.addObserver(self, forKeyPath:"estimatedProgress", options: NSKeyValueObservingOptions.new, context:nil)
  30. webView.navigationDelegate = self
  31. // webview
  32. view.addSubview(webView)
  33. webView.snp.makeConstraints { (make)in
  34. make.edges.equalTo(0)
  35. }
  36. // progressview
  37. view.addSubview(progressView)
  38. progressView.snp.makeConstraints { (make)in
  39. make.width.equalToSuperview()
  40. make.height.equalTo(3)
  41. make.top.equalToSuperview()
  42. }
  43. progressView.tintColor = QSLColor.Color_202020
  44. progressView.isHidden = true
  45. // load url
  46. if webUrl != nil {
  47. webView.load(URLRequest(url:URL(string: webUrl!)!))
  48. }
  49. }
  50. func updateTop() {
  51. topView.titleLabel.text = self.title
  52. topView.backBtnBlock = {
  53. self.dismiss(animated: true)
  54. }
  55. view.addSubview(topView)
  56. topView.snp.makeConstraints { make in
  57. make.left.top.right.equalTo(0)
  58. make.height.equalTo(44)
  59. }
  60. webView.snp.remakeConstraints { make in
  61. make.top.equalTo(topView.snp.bottom)
  62. make.left.right.bottom.equalTo(0)
  63. }
  64. progressView.snp.remakeConstraints { (make)in
  65. make.width.equalToSuperview()
  66. make.height.equalTo(3)
  67. make.top.equalTo(topView.snp.bottom)
  68. }
  69. }
  70. override func viewWillAppear(_ animated:Bool) {
  71. // super.viewWillAppear(animated)
  72. self.navigationController?.setNavigationBarHidden(false, animated: animated)
  73. }
  74. override func viewDidDisappear(_ animated: Bool) {
  75. super.viewDidDisappear(animated)
  76. self.navigationController?.setNavigationBarHidden(true, animated: animated)
  77. }
  78. override func observeValue(forKeyPath keyPath:String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  79. // 加载进度
  80. if keyPath == #keyPath(WKWebView.estimatedProgress) {
  81. let progress = Float(webView.estimatedProgress)
  82. // TODO: 更新进度条等UI,例如:
  83. progressView.progress = progress
  84. }
  85. }
  86. func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  87. progressView.isHidden = false
  88. progressView.progress = 0
  89. }
  90. func webView(_ webView:WKWebView, didFinish navigation: WKNavigation!) {
  91. progressView.isHidden = true
  92. progressView.setProgress(0, animated:false)
  93. }
  94. func webView(_ webView:WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
  95. progressView.isHidden = true
  96. progressView.setProgress(0, animated:false)
  97. }
  98. }
  99. class QSLWebViewTopView: UIView {
  100. lazy var closeBtn: UIButton = {
  101. let btn = UIButton()
  102. btn.image(UIImage(named: "common_close_btn"))
  103. btn.addTarget(self, action: #selector(closeBtnAction), for: .touchUpInside)
  104. return btn
  105. }()
  106. lazy var titleLabel: UILabel = {
  107. let label = UILabel()
  108. label.boldFont(18)
  109. label.textColor = QSLColor.Color_202020
  110. return label
  111. }()
  112. var backBtnBlock: (() -> ())?
  113. override init(frame: CGRect) {
  114. super.init(frame: frame)
  115. addSubview(closeBtn)
  116. addSubview(titleLabel)
  117. closeBtn.snp.makeConstraints { make in
  118. make.size.equalTo(CGSize(width: 24, height: 24))
  119. make.right.equalTo(-12)
  120. make.centerY.equalToSuperview()
  121. }
  122. titleLabel.snp.makeConstraints { make in
  123. make.center.equalToSuperview()
  124. }
  125. }
  126. required init?(coder: NSCoder) {
  127. fatalError("init(coder:) has not been implemented")
  128. }
  129. @objc func closeBtnAction() {
  130. if let backBtnBlock = self.backBtnBlock {
  131. backBtnBlock()
  132. }
  133. }
  134. }