AgreementVC.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //
  2. // ViewController.swift
  3. // WisdomCampusApk
  4. //
  5. // Created by 方楚豪 on 2021/11/21.
  6. //
  7. import UIKit
  8. import Toast_Swift
  9. import SnapKit
  10. import MMKV
  11. import APNGKit
  12. import JFPopup
  13. import WebKit
  14. class AgreementVC: UIViewController {
  15. static let ENTER_KEY:String = "enterMain"
  16. static let AGREE_KEY:String = "wcAgreement"
  17. let agreeButton = UIButton()
  18. var width = 0.0
  19. var height = 0.0
  20. var mmkv:MMKV = MMKV.default()!
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. self.navigationController?.navigationBar.isHidden = true
  24. self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
  25. if mmkv.contains(key: AgreementVC.ENTER_KEY) {
  26. let entryCampusAssistantIndex = mmkv.bool(forKey: AgreementVC.ENTER_KEY)
  27. if entryCampusAssistantIndex == true {
  28. DispatchQueue.main.asyncAfter(deadline: .now()) {
  29. AppUtils.shared.showMainController()
  30. }
  31. }
  32. }
  33. self.view.backgroundColor = .white
  34. self.width = self.view.frame.width
  35. self.height = self.view.frame.height
  36. print("屏幕尺寸为:宽度:\(width),高度:\(height)")
  37. let topBackgroundWidth = 375*width/375
  38. let topBackgroundHeight = 440*width/375
  39. let top = UIView(frame: CGRect(x: 0, y: 0, width: Int(topBackgroundWidth), height: Int(topBackgroundHeight)))
  40. let topBackgroundImageView = UIImageView(frame: top.frame)
  41. topBackgroundImageView.image = UIImage(named:"静态BG@3x.png")
  42. topBackgroundImageView.contentMode = .scaleAspectFill
  43. top.addSubview(topBackgroundImageView)
  44. do {
  45. let topImageWidth = 375*width/375
  46. let topImageHeight = 286*width/375
  47. let topImageYDistance = 124.5*width/375
  48. let image = try APNGImage(named: "dct_00_iSpt")
  49. let topImageView = APNGImageView(image: image)
  50. topImageView.frame = CGRect(x: 0, y: Int(topImageYDistance), width: Int(topImageWidth), height: Int(topImageHeight))
  51. top.addSubview(topImageView)
  52. }catch {
  53. }
  54. self.view.addSubview(top)
  55. let enterButtonWidth = 280*width/375
  56. let enterButtonHeight = 44*width/375
  57. let enterButtonXDistance = (width - enterButtonWidth)/2
  58. let enterButtonYDistance = height - 100*width/375
  59. let enterButtonView = UIView(frame: CGRect(x: enterButtonXDistance, y: enterButtonYDistance, width: enterButtonWidth, height: enterButtonHeight))
  60. let enterButton = UIButton()
  61. enterButton.frame = CGRect(x: 0, y: 0, width: enterButtonWidth, height: enterButtonHeight)
  62. enterButton.setBackgroundImage(UIImage(named: "登录按钮@2x.png"), for: .normal)
  63. enterButton.imageView?.contentMode = .scaleAspectFill
  64. enterButton.titleLabel?.font = UIFont.init(name: "PingFang-SC-Medium", size: 18*width/375)//文字大小
  65. enterButton.setTitle("进入智慧校园", for: .normal) //按钮文字
  66. enterButton.setTitleColor(UIColor.white, for: .normal) //文字颜色
  67. enterButtonView.addSubview(enterButton);
  68. enterButton.addTarget(self, action: #selector(enterButtonClicked), for: .touchUpInside)
  69. self.view.addSubview(enterButtonView)
  70. let agreeButtonSideLength = 15*width/375
  71. let agreeButtonXDistance = 90*width/375
  72. let agreeButtonYDistance = enterButtonYDistance + enterButtonHeight + 15*width/375
  73. let agreeButtonView = UIView(frame: CGRect(x: agreeButtonXDistance, y: agreeButtonYDistance, width: agreeButtonSideLength, height: agreeButtonSideLength))
  74. agreeButton.frame = CGRect(x: 0, y: 0, width: agreeButtonSideLength, height: agreeButtonSideLength)
  75. agreeButton.setBackgroundImage(UIImage(named: "未打钩@2x.png"), for: .normal)
  76. if mmkv.contains(key: AgreementVC.AGREE_KEY) {
  77. let agreeAllProtocol = mmkv.bool(forKey: AgreementVC.AGREE_KEY)
  78. if (agreeAllProtocol == true) {
  79. agreeButton.setBackgroundImage(UIImage(named: "打钩@2x.png"), for: .normal)
  80. }
  81. }
  82. agreeButtonView.addSubview(agreeButton)
  83. agreeButton.addTarget(self, action: #selector(agreeButtonClicked), for: .touchUpInside)
  84. self.view.addSubview(agreeButtonView)
  85. let agreeTextWidth = 190*width/375
  86. let agreeTextHeight = 20*width/375
  87. let agreeTextXDistance = agreeButtonXDistance + agreeButtonSideLength
  88. let agreeTextYDistance = agreeButtonYDistance
  89. let agreeTextView = UITextView(frame: CGRect(x: agreeTextXDistance, y: agreeTextYDistance, width: agreeTextWidth, height: agreeTextHeight))
  90. agreeTextView.dataDetectorTypes = .all
  91. agreeTextView.isEditable = false
  92. agreeTextView.isScrollEnabled = false
  93. agreeTextView.isSelectable = false
  94. agreeTextView.textContainerInset.top = 0
  95. agreeTextView.backgroundColor = .white
  96. self.view.addSubview(agreeTextView)
  97. //UIlabel的富⽂本设置
  98. let attributeString = NSMutableAttributedString.init(string: "我已阅读并同意服务协议和隐私协议")
  99. let pstyle = NSMutableParagraphStyle()
  100. pstyle.alignment = NSTextAlignment.left
  101. attributeString.addAttribute(NSAttributedString.Key.paragraphStyle, value: pstyle, range: NSMakeRange(0, attributeString.length))
  102. //设置字体颜色
  103. attributeString.addAttribute(NSAttributedString.Key.foregroundColor, value: "3f4041".uicolor(), range: NSMakeRange(0, attributeString.length))
  104. //文本所有字符字体PingFang-SC-Regular,字体大小11*width/375
  105. attributeString.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "PingFang-SC-Regular", size: 11*width/375)!, range: NSMakeRange(0, attributeString.length))
  106. //文本7开始4个字符字体PingFang-SC-Regular,字体大小11*width/375
  107. attributeString.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "PingFang-SC-Regular", size: 11*width/375)!, range: NSMakeRange(7, 4))
  108. //设置字体颜色
  109. attributeString.addAttribute(NSAttributedString.Key.foregroundColor, value: "4c77ff".uicolor(), range: NSMakeRange(7, 4))
  110. //文本12开始4个字符字体PingFang-SC-Regular,字体大小11*width/375
  111. attributeString.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "PingFang-SC-Regular", size: 11*width/375)!, range: NSMakeRange(12, 4))
  112. //设置字体颜色
  113. attributeString.addAttribute(NSAttributedString.Key.foregroundColor, value: "4c77ff".uicolor(), range: NSMakeRange(12, 4))
  114. //文本12开始4个字符字体PingFang-SC-Regular,字体大小11*width/375
  115. attributeString.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "PingFang-SC-Regular", size: 11*width/375)!, range: NSMakeRange(11, 1))
  116. //设置字体颜色
  117. attributeString.addAttribute(NSAttributedString.Key.foregroundColor, value: "585858".uicolor(), range: NSMakeRange(11, 1))
  118. agreeTextView.attributedText = attributeString
  119. agreeTextView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(agreeViewTextClick)))
  120. let webView = WKWebView(frame: .zero,configuration: WKWebViewConfiguration())
  121. let myURL = URL(string: "http://www.baidu.com")!
  122. let myRequest = URLRequest(url: myURL)
  123. webView.load(myRequest)
  124. }
  125. @objc func enterButtonClicked() {
  126. if mmkv.contains(key: AgreementVC.AGREE_KEY) {
  127. let agree = mmkv.bool(forKey: AgreementVC.AGREE_KEY)
  128. if agree == true {
  129. mmkv.set(true, forKey: AgreementVC.ENTER_KEY)
  130. DispatchQueue.main.asyncAfter(deadline: .now()) {
  131. //show main vc
  132. AppUtils.shared.showMainController()
  133. }
  134. return
  135. }
  136. }
  137. self.view.makeToast("请先同意协议")
  138. }
  139. @objc func agreeButtonClicked() {
  140. if mmkv.contains(key: AgreementVC.AGREE_KEY) {
  141. let agree = mmkv.bool(forKey: AgreementVC.AGREE_KEY)
  142. if agree == true {
  143. agreeButton.setBackgroundImage(UIImage(named: "未打钩@2x.png"), for: .normal)
  144. mmkv.set(false, forKey: AgreementVC.AGREE_KEY)
  145. return
  146. }
  147. }
  148. agreeButton.setBackgroundImage(UIImage(named: "打钩@2x.png"), for: .normal)
  149. mmkv.set(true, forKey: AgreementVC.AGREE_KEY)
  150. }
  151. @objc func agreeViewTextClick(recognizer: UIGestureRecognizer) {
  152. let textView: UITextView = recognizer.view as! UITextView
  153. let layoutManager: NSLayoutManager = textView.layoutManager
  154. var location: CGPoint = recognizer.location(in: textView)
  155. location.x -= textView.textContainerInset.left
  156. location.y -= textView.textContainerInset.top
  157. var charIndex: Int
  158. charIndex = layoutManager.characterIndex(for: location, in: textView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
  159. if charIndex < textView.textStorage.length {
  160. if charIndex >= 7 && charIndex < 11 {
  161. self.popup.dialog {
  162. return getDialog(filename: "服务协议")
  163. }
  164. }else if (charIndex >= 12 && charIndex < 16) {
  165. self.popup.dialog {
  166. return getDialog(filename: "隐私协议")
  167. }
  168. }
  169. }
  170. }
  171. func getDialog(filename: String) -> UIView {
  172. let myDialog = UIView(frame: CGRect(x: 0, y: 0, width: 300*width/375, height: 500*width/375))
  173. myDialog.backgroundColor = .white
  174. let titleLabel = UILabel.init(frame: CGRect(x: 0, y: 10*width/375, width: 300*width/375, height: 20*width/375))
  175. titleLabel.text = filename
  176. titleLabel.font = UIFont.init(name: "PingFang-SC-Medium", size: 20*width/375)//文字大小
  177. titleLabel.textColor = "3f4041".uicolor()
  178. titleLabel.textAlignment = .center
  179. // 协议内容
  180. let content = getTextFileStr(filename: filename)
  181. let contentScrollView = UIScrollView.init(frame: CGRect(x: 0, y: 40*width/375, width: 300*width/375, height: 450*width/375))
  182. contentScrollView.contentSize = CGSize.init(width: 300*width/375, height: 3250*width/375)
  183. contentScrollView.showsVerticalScrollIndicator = false
  184. let contentLabel = UILabel.init(frame: CGRect(x: 12*width/375, y: 0, width: 280*width/375, height: 450*width/375))
  185. contentLabel.numberOfLines = 0
  186. contentLabel.text = content
  187. contentLabel.font = UIFont.init(name: "PingFang-SC-Medium", size: 16*width/375)//文字大小
  188. contentLabel.textColor = "3f4041".uicolor()
  189. contentLabel.sizeToFit()
  190. contentScrollView.addSubview(contentLabel)
  191. myDialog.addSubview(contentScrollView)
  192. myDialog.addSubview(titleLabel)
  193. myDialog.layer.cornerRadius = 10
  194. return myDialog
  195. }
  196. func getTextFileStr(filename:String!) -> String! {
  197. if let path = Bundle.main.path(forResource: filename, ofType: "txt") {
  198. do {
  199. let data = try String(contentsOfFile: path, encoding: .utf8)
  200. return data
  201. } catch {
  202. print(error)
  203. }
  204. }
  205. return ""
  206. }
  207. }
  208. extension String {
  209. /// 十六进制字符串颜色转为UIColor
  210. /// - Parameter alpha: 透明度
  211. func uicolor(alpha: CGFloat = 1.0) -> UIColor {
  212. // 存储转换后的数值
  213. var red: UInt64 = 0, green: UInt64 = 0, blue: UInt64 = 0
  214. var hex = self
  215. // 如果传入的十六进制颜色有前缀,去掉前缀
  216. if hex.hasPrefix("0x") || hex.hasPrefix("0X") {
  217. hex = String(hex[hex.index(hex.startIndex, offsetBy: 2)...])
  218. } else if hex.hasPrefix("#") {
  219. hex = String(hex[hex.index(hex.startIndex, offsetBy: 1)...])
  220. }
  221. // 如果传入的字符数量不足6位按照后边都为0处理,当然你也可以进行其它操作
  222. if hex.count < 6 {
  223. for _ in 0..<6-hex.count {
  224. hex += "0"
  225. }
  226. }
  227. // 分别进行转换
  228. // 红
  229. Scanner(string: String(hex[..<hex.index(hex.startIndex, offsetBy: 2)])).scanHexInt64(&red)
  230. // 绿
  231. Scanner(string: String(hex[hex.index(hex.startIndex, offsetBy: 2)..<hex.index(hex.startIndex, offsetBy: 4)])).scanHexInt64(&green)
  232. // 蓝
  233. Scanner(string: String(hex[hex.index(startIndex, offsetBy: 4)...])).scanHexInt64(&blue)
  234. return UIColor(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: alpha)
  235. }
  236. }