杨锴
2024-11-07 62a24b3c7cf92919a93ee575e9460037e1a53816
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
//  WebVC.swift
//  XQMuse
//
//  Created by 无故事王国 on 2024/8/13.
//
 
import UIKit
import WebKit
import JQTools
 
class WebVC: BaseVC {
 
                private var webView:WKWebView?
                private var type:AgreementType?
                private(set) var url:String?
                private(set) var htmlText:String?
                private(set) var baseUrl:URL?
                private var progressView = UIProgressView()
                private let jsCode = """
                                var meta = document.createElement('meta');"
                                "meta.name = 'viewport';"
                                "meta.content = 'width=device-width, initial-scale=1.0,minimum-scale=1.0, maximum-scale=1.0, user-scalable=yes';"
                                "document.getElementsByTagName('head')[0].appendChild(meta);
                """
 
                public var tintColor = UIColor.blue
 
                public convenience init(url:String) {
                                self.init()
                                self.url = url
                }
 
                public convenience init(type:AgreementType){
                                self.init()
                                self.type = type
                }
 
                public convenience init(htmlText:String,baseURL:URL? = nil) {
                                self.init()
                                self.htmlText = htmlText.jq_wrapHtml()
                                self.baseUrl = baseURL
                }
 
                public override func viewDidLoad() {
                                super.viewDidLoad()
 
                                let config = WKWebViewConfiguration()
                                let userScript = WKUserScript(source: jsCode, injectionTime: .atDocumentStart, forMainFrameOnly: true)
                                let content = WKUserContentController()
                                content.addUserScript(userScript)
                                config.userContentController = content
                                webView = WKWebView(frame: CGRect.zero, configuration: config)
                                webView?.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
                                view.addSubview(webView!)
                                webView?.snp.makeConstraints({ (make) in
            make.top.equalToSuperview().offset(UIDevice.jq_safeEdges.top)
                                                make.left.right.bottom.equalToSuperview()
                                })
 
                                if #available(iOS 11.0, *) {
                                                webView!.scrollView.contentInsetAdjustmentBehavior = .automatic
                                } else {
                                                self.automaticallyAdjustsScrollViewInsets = false
                                }
 
        webView!.scrollView.delegate = self
 
                                progressView.tintColor = tintColor
                                view.addSubview(progressView)
                                progressView.snp.makeConstraints { (make) in
                                                make.top.left.right.equalToSuperview()
                                                make.height.equalTo(2)
                                }
 
                                if type != nil{
                                                Services.agreementBy(type!).subscribe(onNext: {data in
                                                                if let model = data.data{
                                                                                self.webView?.loadHTMLString(model.content.jq_wrapHtml(), baseURL: nil)
                                                                }
                                                }).disposed(by: disposeBag)
                                }
 
                                if url != nil {
                                                let urlRequest = URLRequest(url: URL(string: url!)!)
                                                webView?.load(urlRequest)
                                                return
                                }
 
                                if htmlText != nil{
                                                webView?.loadHTMLString(htmlText!, baseURL: baseUrl)
                                }
                }
 
                public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
                                let value = change![NSKeyValueChangeKey(rawValue: "new")] as! Double
                                print(value)
 
 
                                progressView.setProgress(Float(value), animated: true)
                                if value == 1.0 {
                                                progressView.isHidden = true
                                }else{
                                                progressView.isHidden = false
                                }
                }
 
                deinit {
                                webView?.removeObserver(self, forKeyPath: "estimatedProgress")
                }
 
}
 
extension WebVC:UIScrollViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //                                let v = min(scrollView.contentOffset.y / JQ_NavBarHeight, 1)
        //                                navigationController?.navigationBar.standardAppearance.backgroundColor = .white.withAlphaComponent(v)
        navigationController?.navigationBar.standardAppearance.backgroundColor = .white
        navigationController?.navigationBar.scrollEdgeAppearance?.backgroundColor = .white
    }
}