QSLVipAlertView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //
  2. // QSLVipAlertView.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2025/7/28.
  6. //
  7. import UIKit
  8. class QSLVipAlertView: UIView {
  9. lazy var contentView: UIView = {
  10. let contentViewW = QSLConst.qsl_kScreenW - 60.rpx
  11. let contentViewH = 152.0.rpx
  12. let contentView = UIView(frame: CGRect(x: 0, y: 0, width: contentViewW, height: contentViewH))
  13. contentView.backgroundColor = .white
  14. contentView.addRadius(radius: 8.rpx)
  15. contentView.addBorder(borderWidth: 3.rpx, borderColor: .hexStringColor(hexString: "#FFFFFF"))
  16. return contentView
  17. }()
  18. lazy var centerImage : UIImageView = {
  19. let centerImage = UIImageView()
  20. centerImage.contentMode = .scaleAspectFit
  21. centerImage.image = UIImage(named: "vip_comment_give_thumb")
  22. return centerImage
  23. }()
  24. //渐变色
  25. lazy var headerColorView: UIView = {
  26. let headerColorViewW = QSLConst.qsl_kScreenW - 60.rpx
  27. let headerColorView = UIView(frame: CGRect(x: 0, y: 0, width: headerColorViewW, height: 100))
  28. headerColorView.layer.masksToBounds = true
  29. // 创建渐变图层
  30. let gradientLayer = CAGradientLayer()
  31. gradientLayer.frame = headerColorView.bounds
  32. // 设置渐变颜色 (#9EFFEC 到白色)
  33. gradientLayer.colors = [
  34. UIColor(red: 0.62, green: 1.0, blue: 0.93, alpha: 1.0).cgColor, // #9EFFEC
  35. UIColor.white.cgColor
  36. ]
  37. // 设置渐变方向 (180deg 表示从上到下)
  38. gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.0) // 顶部中点
  39. gradientLayer.endPoint = CGPoint(x: 0.5, y: 1.0) // 底部中点
  40. // 添加渐变图层到视图
  41. headerColorView.layer.insertSublayer(gradientLayer, at: 0)
  42. return headerColorView
  43. }()
  44. lazy var titleLabel: UILabel = {
  45. let label = UILabel()
  46. label.text("给我们一个好评呗~")
  47. label.mediumFont(20)
  48. label.textColor = QSLColor.textColor_333
  49. return label
  50. }()
  51. ///中间描述区域
  52. lazy var middleView : UIView = {
  53. let middleView = UIView()
  54. middleView.backgroundColor = .white
  55. return middleView
  56. }()
  57. lazy var leftMiddleView : UIView = {
  58. let leftMiddleView = UIView(frame: CGRectMake(0, 0, 14.rpx, 1.rpx))
  59. leftMiddleView.gradientBackgroundColor(color1: .hexStringColor(hexString: "#AAAAAA",alpha: 0.0), color2: .hexStringColor(hexString: "#AAAAAA"), width: 14.rpx, height: 1.rpx, direction: .horizontal)
  60. return leftMiddleView
  61. }()
  62. lazy var rithMiddleView : UIView = {
  63. let rithMiddleView = UIView(frame: CGRectMake(0, 0, 14, 1))
  64. rithMiddleView.gradientBackgroundColor(color1: .hexStringColor(hexString: "#AAAAAA"), color2: .hexStringColor(hexString: "#AAAAAA",alpha: 0.0), width: 14.rpx, height: 1.rpx, direction: .horizontal)
  65. return rithMiddleView
  66. }()
  67. lazy var contentLabel: UILabel = {
  68. let label = UILabel()
  69. label.numberOfLines = 0
  70. label.textAlignment = .center
  71. label.text("登录之后才可以发送好友申请")
  72. label.font(12)
  73. label.textColor = .hexStringColor(hexString: "#00000099" ,alpha: 0.4)
  74. label.changeLineSpace(space: 4)
  75. return label
  76. }()
  77. lazy var oneButton: UIButton = {
  78. let btn = UIButton()
  79. btn.addRadius(radius: 20.rpx)
  80. btn.title("去登录")
  81. btn.textColor(.white)
  82. btn.mediumFont(16)
  83. btn.gradientBackgroundColor(color1: .hexStringColor(hexString: "#15CBA1"), color2: .hexStringColor(hexString: "#1FE0BA"), width: 150.rpx, height: 40.rpx, direction: .horizontal)
  84. btn.addTarget(self, action: #selector(oneBtnAction), for: .touchUpInside)
  85. return btn
  86. }()
  87. lazy var firstButton: UIButton = {
  88. let btn = UIButton()
  89. btn.isHidden = true
  90. btn.backgroundColor = .hexStringColor(hexString: "#F8F8F8")
  91. btn.addRadius(radius: 20.rpx)
  92. btn.title("取消")
  93. btn.textColor(.hexStringColor(hexString: "#A7A7A7"))
  94. btn.mediumFont(16)
  95. btn.addTarget(self, action: #selector(firstBtnAction), for: .touchUpInside)
  96. return btn
  97. }()
  98. lazy var secondButton: UIButton = {
  99. let btn = UIButton()
  100. btn.isHidden = true
  101. btn.addRadius(radius: 20.rpx)
  102. btn.title("确认")
  103. btn.textColor(.white)
  104. btn.mediumFont(16)
  105. btn.gradientBackgroundColor(color1: .hexStringColor(hexString: "#15CBA1"), color2: .hexStringColor(hexString: "#1FE0BA"), width: 118.rpx, height: 40.rpx, direction: .horizontal)
  106. btn.addTarget(self, action: #selector(secondBtnAction), for: .touchUpInside)
  107. return btn
  108. }()
  109. lazy var closeButton: UIButton = {
  110. let btn = UIButton()
  111. btn.setBackgroundImage(UIImage(named: "public_btn_close_AAA"), for: .normal)
  112. btn.addTarget(self, action: #selector(closeBtnAction), for: .touchUpInside)
  113. return btn
  114. }()
  115. var oneBtnClosure: (() -> ())?
  116. var firstBtnClosure: (() -> ())?
  117. var secondBtnClosure: (() -> ())?
  118. var closeBtnClosure: (() -> ())?
  119. class func alert(view: UIView,
  120. title: String,
  121. titleAttring: NSAttributedString,
  122. content: String,
  123. isOneBtn: Bool = false,
  124. oneBtnText: String = "去登录",
  125. oneBtnClosure: @escaping () -> () = {},
  126. firstBtnClosure: @escaping () -> () = {},
  127. secondBtnClosure: @escaping () -> () = {},
  128. closeBtnClosure: @escaping () -> () = {}) {
  129. let window = QSLVipAlertView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))
  130. if title.count > 0 {
  131. window.titleLabel.text = title
  132. } else {
  133. window.titleLabel.attributedText = titleAttring
  134. }
  135. window.contentLabel.text = content
  136. window.contentLabel.changeLineSpace(space: 4)
  137. window.oneButton.title(oneBtnText)
  138. window.oneButton.isHidden = !isOneBtn
  139. window.firstButton.isHidden = isOneBtn
  140. window.secondButton.isHidden = isOneBtn
  141. window.oneBtnClosure = oneBtnClosure
  142. window.firstBtnClosure = firstBtnClosure
  143. window.secondBtnClosure = secondBtnClosure
  144. window.closeBtnClosure = closeBtnClosure
  145. let contentHeight = content.heightAccording(width: QSLConst.qsl_kScreenW - 108.rpx, font: UIFont.textF(15), lineSpacing: 4)
  146. window.contentView.snp.remakeConstraints { make in
  147. make.size.equalTo(CGSize(width: QSLConst.qsl_kScreenW - 60.rpx, height: 65.rpx + contentHeight + 129.rpx))
  148. make.center.equalToSuperview()
  149. }
  150. view.addSubview(window)
  151. UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 0.05) {
  152. window.backgroundColor = .hexStringColor(hexString: "#000000", alpha: 0.7)
  153. window.contentView.isHidden = false
  154. }
  155. }
  156. override init(frame: CGRect) {
  157. super.init(frame: frame)
  158. initView()
  159. }
  160. required init?(coder: NSCoder) {
  161. fatalError("init(coder:) has not been implemented")
  162. }
  163. // 单按钮点击事件
  164. @objc func oneBtnAction() {
  165. if let oneBtnClosure = self.oneBtnClosure {
  166. oneBtnClosure()
  167. }
  168. removeView()
  169. }
  170. // 取消按钮点击事件
  171. @objc func firstBtnAction() {
  172. if let firstBtnClosure = self.firstBtnClosure {
  173. firstBtnClosure()
  174. }
  175. removeView()
  176. }
  177. // 确认按钮点击事件
  178. @objc func secondBtnAction() {
  179. if let secondBtnClosure = self.secondBtnClosure {
  180. secondBtnClosure()
  181. }
  182. removeView()
  183. }
  184. // 关闭按钮点击事件
  185. @objc func closeBtnAction() {
  186. if let closeBtnClosure = self.closeBtnClosure {
  187. closeBtnClosure()
  188. }
  189. removeView()
  190. }
  191. // 移除
  192. @objc func removeView() {
  193. UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 0.05) { [weak self] in
  194. self?.backgroundColor = UIColor.init(white: 0, alpha: 0)
  195. self?.contentView.isHidden = true
  196. self?.centerImage.isHidden = true
  197. } completion: { [weak self] finished in
  198. self?.removeFromSuperview()
  199. }
  200. }
  201. }
  202. extension QSLVipAlertView {
  203. func initView() {
  204. addSubview(contentView)
  205. contentView.snp.makeConstraints { make in
  206. make.size.equalTo(CGSize(width: QSLConst.qsl_kScreenW - 60.rpx, height: 152.0.rpx))
  207. make.center.equalToSuperview()
  208. }
  209. addSubview(centerImage)
  210. centerImage.snp.makeConstraints { make in
  211. make.centerX.equalToSuperview()
  212. make.top.equalTo(contentView.snp.top).offset(-40.rpx)
  213. make.size.equalTo(CGSizeMake(164.6.rpx, 114.rpx))
  214. }
  215. contentView.addSubview(headerColorView)
  216. headerColorView.snp.makeConstraints { make in
  217. make.top.equalToSuperview()
  218. make.left.equalToSuperview()
  219. make.right.equalToSuperview()
  220. make.height.equalTo(100)
  221. }
  222. contentView.addSubview(titleLabel)
  223. titleLabel.snp.makeConstraints { make in
  224. make.centerX.equalToSuperview()
  225. make.top.equalTo(68.rpx)
  226. }
  227. //中间区域
  228. contentView.addSubview(middleView)
  229. middleView.snp.makeConstraints { make in
  230. make.top.equalTo(titleLabel.snp.bottom).offset(16.5.rpx)
  231. make.left.equalTo(0)
  232. make.right.equalTo(0)
  233. make.height.equalTo(21.rpx)
  234. }
  235. middleView.addSubview(contentLabel)
  236. contentLabel.snp.makeConstraints { make in
  237. make.center.equalToSuperview()
  238. make.height.equalTo(21.rpx)
  239. }
  240. middleView.addSubview(leftMiddleView)
  241. leftMiddleView.snp.makeConstraints { make in
  242. make.right.equalTo(contentLabel.snp.left).offset(-6.rpx)
  243. make.centerY.equalToSuperview()
  244. make.size.equalTo(CGSizeMake(14.rpx, 1.rpx))
  245. }
  246. middleView.addSubview(rithMiddleView)
  247. rithMiddleView.snp.makeConstraints { make in
  248. make.left.equalTo(contentLabel.snp.right).offset(6.rpx)
  249. make.centerY.equalToSuperview()
  250. make.size.equalTo(CGSizeMake(14.rpx, 1.rpx))
  251. }
  252. contentView.addSubview(oneButton)
  253. oneButton.snp.makeConstraints { make in
  254. make.size.equalTo(CGSize(width: 150.rpx, height: 40.rpx))
  255. make.centerX.equalToSuperview()
  256. make.bottom.equalTo(-24.rpx)
  257. }
  258. contentView.addSubview(firstButton)
  259. firstButton.snp.makeConstraints { make in
  260. make.size.equalTo(CGSize(width: 118.rpx, height: 40.rpx))
  261. make.left.equalTo(24.rpx)
  262. make.bottom.equalTo(-24.rpx)
  263. }
  264. contentView.addSubview(secondButton)
  265. secondButton.snp.makeConstraints { make in
  266. make.size.equalTo(CGSize(width: 118.rpx, height: 40.rpx))
  267. make.right.equalTo(-24.rpx)
  268. make.bottom.equalTo(-24.rpx)
  269. }
  270. contentView.addSubview(closeButton)
  271. closeButton.snp.makeConstraints { make in
  272. make.size.equalTo(CGSize(width: 24.rpx, height: 24.rpx))
  273. make.top.equalTo(12.rpx)
  274. make.right.equalTo(-12.rpx)
  275. }
  276. }
  277. }