QSLAlertView.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // QSLAlertView.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2024/12/4.
  6. //
  7. import UIKit
  8. class QSLAlertView: UIView {
  9. lazy var contentView: UIView = {
  10. let contentView = UIView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW - 60.rpx, height: 152.0.rpx))
  11. contentView.backgroundColor = .white
  12. contentView.addRadius(radius: 8.rpx)
  13. return contentView
  14. }()
  15. lazy var titleLabel: UILabel = {
  16. let label = UILabel()
  17. label.text("温馨提示")
  18. label.mediumFont(17)
  19. label.textColor = QSLColor.Color_202020
  20. return label
  21. }()
  22. lazy var contentLabel: UILabel = {
  23. let label = UILabel()
  24. label.numberOfLines = 0
  25. label.text("登录之后才可以发送好友申请")
  26. label.font(14)
  27. label.textColor = .hexStringColor(hexString: "#404040")
  28. label.changeLineSpace(space: 4)
  29. return label
  30. }()
  31. lazy var oneButton: UIButton = {
  32. let btn = UIButton()
  33. btn.addRadius(radius: 20.rpx)
  34. btn.title("去登录")
  35. btn.textColor(.white)
  36. btn.mediumFont(16)
  37. btn.gradientBackgroundColor(color1: .hexStringColor(hexString: "#15CBA1"), color2: .hexStringColor(hexString: "#1FE0BA"), width: 150.rpx, height: 40.rpx, direction: .horizontal)
  38. btn.addTarget(self, action: #selector(oneBtnAction), for: .touchUpInside)
  39. return btn
  40. }()
  41. lazy var firstButton: UIButton = {
  42. let btn = UIButton()
  43. btn.isHidden = true
  44. btn.backgroundColor = .hexStringColor(hexString: "#F8F8F8")
  45. btn.addRadius(radius: 20.rpx)
  46. btn.title("取消")
  47. btn.textColor(.hexStringColor(hexString: "#A7A7A7"))
  48. btn.mediumFont(16)
  49. btn.addTarget(self, action: #selector(firstBtnAction), for: .touchUpInside)
  50. return btn
  51. }()
  52. lazy var secondButton: UIButton = {
  53. let btn = UIButton()
  54. btn.isHidden = true
  55. btn.addRadius(radius: 20.rpx)
  56. btn.title("确认")
  57. btn.textColor(.white)
  58. btn.mediumFont(16)
  59. btn.gradientBackgroundColor(color1: .hexStringColor(hexString: "#15CBA1"), color2: .hexStringColor(hexString: "#1FE0BA"), width: 118.rpx, height: 40.rpx, direction: .horizontal)
  60. btn.addTarget(self, action: #selector(secondBtnAction), for: .touchUpInside)
  61. return btn
  62. }()
  63. lazy var closeButton: UIButton = {
  64. let btn = UIButton()
  65. btn.setBackgroundImage(UIImage(named: "public_btn_close_AAA"), for: .normal)
  66. btn.addTarget(self, action: #selector(closeBtnAction), for: .touchUpInside)
  67. return btn
  68. }()
  69. var oneBtnClosure: (() -> ())?
  70. var firstBtnClosure: (() -> ())?
  71. var secondBtnClosure: (() -> ())?
  72. var closeBtnClosure: (() -> ())?
  73. class func alert(view: UIView,
  74. title: String,
  75. content: String,
  76. isOneBtn: Bool = false,
  77. contentTextAlignment: NSTextAlignment = .center,
  78. oneBtnText: String = "去登录",
  79. oneBtnClosure: @escaping () -> () = {},
  80. firstBtnClosure: @escaping () -> () = {},
  81. secondBtnClosure: @escaping () -> () = {},
  82. closeBtnClosure: @escaping () -> () = {}) {
  83. let window = QSLAlertView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))
  84. window.titleLabel.text = title
  85. window.contentLabel.text = content
  86. window.contentLabel.changeLineSpace(space: 4)
  87. window.contentLabel.textAlignment = contentTextAlignment
  88. window.oneButton.title(oneBtnText)
  89. window.oneButton.isHidden = !isOneBtn
  90. window.firstButton.isHidden = isOneBtn
  91. window.secondButton.isHidden = isOneBtn
  92. window.oneBtnClosure = oneBtnClosure
  93. window.firstBtnClosure = firstBtnClosure
  94. window.secondBtnClosure = secondBtnClosure
  95. window.closeBtnClosure = closeBtnClosure
  96. let contentHeight = content.heightAccording(width: QSLConst.qsl_kScreenW - 108.rpx, font: UIFont.textF(15), lineSpacing: 4)
  97. window.contentView.snp.remakeConstraints { make in
  98. make.size.equalTo(CGSize(width: QSLConst.qsl_kScreenW - 60.rpx, height: 65.rpx + contentHeight + 92.0.rpx))
  99. make.center.equalToSuperview()
  100. }
  101. view.addSubview(window)
  102. gravityInstance?.track(QSLGravityConst.friend_delete_show)
  103. UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 0.05) {
  104. window.backgroundColor = .hexStringColor(hexString: "#000000", alpha: 0.7)
  105. window.contentView.isHidden = false
  106. }
  107. }
  108. override init(frame: CGRect) {
  109. super.init(frame: frame)
  110. initView()
  111. }
  112. required init?(coder: NSCoder) {
  113. fatalError("init(coder:) has not been implemented")
  114. }
  115. // 单按钮点击事件
  116. @objc func oneBtnAction() {
  117. if let oneBtnClosure = self.oneBtnClosure {
  118. oneBtnClosure()
  119. }
  120. removeView()
  121. }
  122. // 取消按钮点击事件
  123. @objc func firstBtnAction() {
  124. if let firstBtnClosure = self.firstBtnClosure {
  125. firstBtnClosure()
  126. }
  127. removeView()
  128. }
  129. // 确认按钮点击事件
  130. @objc func secondBtnAction() {
  131. if let secondBtnClosure = self.secondBtnClosure {
  132. secondBtnClosure()
  133. }
  134. removeView()
  135. }
  136. // 关闭按钮点击事件
  137. @objc func closeBtnAction() {
  138. if let closeBtnClosure = self.closeBtnClosure {
  139. closeBtnClosure()
  140. }
  141. removeView()
  142. }
  143. // 移除
  144. @objc func removeView() {
  145. UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 0.05) { [weak self] in
  146. self?.backgroundColor = UIColor.init(white: 0, alpha: 0)
  147. self?.contentView.isHidden = true
  148. } completion: { [weak self] finished in
  149. self?.removeFromSuperview()
  150. }
  151. }
  152. }
  153. extension QSLAlertView {
  154. func initView() {
  155. addSubview(contentView)
  156. contentView.snp.makeConstraints { make in
  157. make.size.equalTo(CGSize(width: QSLConst.qsl_kScreenW - 60.rpx, height: 152.0.rpx))
  158. make.center.equalToSuperview()
  159. }
  160. contentView.addSubview(titleLabel)
  161. titleLabel.snp.makeConstraints { make in
  162. make.centerX.equalToSuperview()
  163. make.top.equalTo(24.rpx)
  164. }
  165. contentView.addSubview(contentLabel)
  166. contentLabel.snp.makeConstraints { make in
  167. make.left.equalTo(24.rpx)
  168. make.right.equalTo(-24.rpx)
  169. make.top.equalTo(65.rpx)
  170. }
  171. contentView.addSubview(oneButton)
  172. oneButton.snp.makeConstraints { make in
  173. make.size.equalTo(CGSize(width: 150.rpx, height: 40.rpx))
  174. make.centerX.equalToSuperview()
  175. make.bottom.equalTo(-24.rpx)
  176. }
  177. contentView.addSubview(firstButton)
  178. firstButton.snp.makeConstraints { make in
  179. make.size.equalTo(CGSize(width: 118.rpx, height: 40.rpx))
  180. make.left.equalTo(24.rpx)
  181. make.bottom.equalTo(-24.rpx)
  182. }
  183. contentView.addSubview(secondButton)
  184. secondButton.snp.makeConstraints { make in
  185. make.size.equalTo(CGSize(width: 118.rpx, height: 40.rpx))
  186. make.right.equalTo(-24.rpx)
  187. make.bottom.equalTo(-24.rpx)
  188. }
  189. contentView.addSubview(closeButton)
  190. closeButton.snp.makeConstraints { make in
  191. make.size.equalTo(CGSize(width: 24.rpx, height: 24.rpx))
  192. make.top.equalTo(12.rpx)
  193. make.right.equalTo(-12.rpx)
  194. }
  195. }
  196. }