QSLAlertView.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. QSEventHandle.eventPush(eventName: 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. class func alertPic(view: UIView,
  109. title: String,
  110. content: String,
  111. isOneBtn: Bool = false,
  112. contentTextAlignment: NSTextAlignment = .center,
  113. oneBtnText: String = "去登录",
  114. oneBtnClosure: @escaping () -> () = {},
  115. firstBtnClosure: @escaping () -> () = {},
  116. secondBtnClosure: @escaping () -> () = {},
  117. closeBtnClosure: @escaping () -> () = {}) {
  118. let window = QSLAlertView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))
  119. window.titleLabel.text = title
  120. window.contentLabel.text = content
  121. window.contentLabel.changeLineSpace(space: 4)
  122. window.contentLabel.textAlignment = contentTextAlignment
  123. window.oneButton.title(oneBtnText)
  124. window.oneButton.isHidden = !isOneBtn
  125. window.firstButton.isHidden = isOneBtn
  126. window.secondButton.isHidden = isOneBtn
  127. window.oneBtnClosure = oneBtnClosure
  128. window.firstBtnClosure = firstBtnClosure
  129. window.secondBtnClosure = secondBtnClosure
  130. window.closeBtnClosure = closeBtnClosure
  131. let contentHeight = content.heightAccording(width: QSLConst.qsl_kScreenW - 108.rpx, font: UIFont.textF(15), lineSpacing: 4)
  132. window.contentView.snp.remakeConstraints { make in
  133. make.size.equalTo(CGSize(width: QSLConst.qsl_kScreenW - 60.rpx, height: 65.rpx + contentHeight + 92.0.rpx))
  134. make.center.equalToSuperview()
  135. }
  136. view.addSubview(window)
  137. QSEventHandle.eventPush(eventName: QSLGravityConst.friend_delete_show)
  138. UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 0.05) {
  139. window.backgroundColor = .hexStringColor(hexString: "#000000", alpha: 0.7)
  140. window.contentView.isHidden = false
  141. }
  142. }
  143. override init(frame: CGRect) {
  144. super.init(frame: frame)
  145. initView()
  146. }
  147. required init?(coder: NSCoder) {
  148. fatalError("init(coder:) has not been implemented")
  149. }
  150. // 单按钮点击事件
  151. @objc func oneBtnAction() {
  152. if let oneBtnClosure = self.oneBtnClosure {
  153. oneBtnClosure()
  154. }
  155. removeView()
  156. }
  157. // 取消按钮点击事件
  158. @objc func firstBtnAction() {
  159. if let firstBtnClosure = self.firstBtnClosure {
  160. firstBtnClosure()
  161. }
  162. removeView()
  163. }
  164. // 确认按钮点击事件
  165. @objc func secondBtnAction() {
  166. if let secondBtnClosure = self.secondBtnClosure {
  167. secondBtnClosure()
  168. }
  169. removeView()
  170. }
  171. // 关闭按钮点击事件
  172. @objc func closeBtnAction() {
  173. if let closeBtnClosure = self.closeBtnClosure {
  174. closeBtnClosure()
  175. }
  176. removeView()
  177. }
  178. // 移除
  179. @objc func removeView() {
  180. UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 0.05) { [weak self] in
  181. self?.backgroundColor = UIColor.init(white: 0, alpha: 0)
  182. self?.contentView.isHidden = true
  183. } completion: { [weak self] finished in
  184. self?.removeFromSuperview()
  185. }
  186. }
  187. }
  188. extension QSLAlertView {
  189. func initView() {
  190. addSubview(contentView)
  191. contentView.snp.makeConstraints { make in
  192. make.size.equalTo(CGSize(width: QSLConst.qsl_kScreenW - 60.rpx, height: 152.0.rpx))
  193. make.center.equalToSuperview()
  194. }
  195. contentView.addSubview(titleLabel)
  196. titleLabel.snp.makeConstraints { make in
  197. make.centerX.equalToSuperview()
  198. make.top.equalTo(24.rpx)
  199. }
  200. contentView.addSubview(contentLabel)
  201. contentLabel.snp.makeConstraints { make in
  202. make.left.equalTo(24.rpx)
  203. make.right.equalTo(-24.rpx)
  204. make.top.equalTo(65.rpx)
  205. }
  206. contentView.addSubview(oneButton)
  207. oneButton.snp.makeConstraints { make in
  208. make.size.equalTo(CGSize(width: 150.rpx, height: 40.rpx))
  209. make.centerX.equalToSuperview()
  210. make.bottom.equalTo(-24.rpx)
  211. }
  212. contentView.addSubview(firstButton)
  213. firstButton.snp.makeConstraints { make in
  214. make.size.equalTo(CGSize(width: 118.rpx, height: 40.rpx))
  215. make.left.equalTo(24.rpx)
  216. make.bottom.equalTo(-24.rpx)
  217. }
  218. contentView.addSubview(secondButton)
  219. secondButton.snp.makeConstraints { make in
  220. make.size.equalTo(CGSize(width: 118.rpx, height: 40.rpx))
  221. make.right.equalTo(-24.rpx)
  222. make.bottom.equalTo(-24.rpx)
  223. }
  224. contentView.addSubview(closeButton)
  225. closeButton.snp.makeConstraints { make in
  226. make.size.equalTo(CGSize(width: 24.rpx, height: 24.rpx))
  227. make.top.equalTo(12.rpx)
  228. make.right.equalTo(-12.rpx)
  229. }
  230. }
  231. }