KeyboardExchangeView.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // KeyboardExchangeView.swift
  3. // AiKeyboard
  4. //
  5. // Created by Destiny on 2025/4/28.
  6. //
  7. import UIKit
  8. protocol KeyboardExchangeViewDelegate: NSObjectProtocol {
  9. func exchangeViewBackClickAction()
  10. func exchangeViewSaveClickAction(keyboardList: [KeyboardModel], success: @escaping (() -> ()))
  11. }
  12. class KeyboardExchangeView: UIView {
  13. struct UX {
  14. static let cellWidth = (KeyboardConst.kb_kScreenW - 42.0) / 3.0
  15. static let cellHeight = 79.0
  16. }
  17. weak var delegate: KeyboardExchangeViewDelegate?
  18. var keyboardList: [KeyboardModel]? {
  19. didSet {
  20. self.collectionView.reloadData()
  21. }
  22. }
  23. lazy var backBtn: UIButton = {
  24. let btn = UIButton()
  25. btn.setImage(UIImage(named: "keyboard_back_btn"), for: .normal)
  26. btn.layer.shadowOffset = CGSize(width: 0, height: 0)
  27. btn.layer.shadowColor = UIColor.hexStringColor(hexString: "#000000", alpha: 0.12).cgColor
  28. btn.layer.shadowRadius = 6.1
  29. btn.addTarget(self, action: #selector(backBtnClickAction), for: .touchUpInside)
  30. return btn
  31. }()
  32. lazy var collectionView: UICollectionView = {
  33. let layout = UICollectionViewFlowLayout()
  34. layout.minimumLineSpacing = 0
  35. layout.scrollDirection = .vertical
  36. let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  37. collectionView.backgroundColor = .clear
  38. collectionView.dataSource = self
  39. collectionView.delegate = self
  40. collectionView.showsVerticalScrollIndicator = false
  41. collectionView.bounces = false
  42. collectionView.register(KeyboardExchangeCell.self, forCellWithReuseIdentifier: KeyboardExchangeCell.reuseIdentifier())
  43. return collectionView
  44. }()
  45. lazy var saveBtn: UIButton = {
  46. let btn = UIButton()
  47. if let image = UIImage.gradient([.hexStringColor(hexString: "#7D46FC"), .hexStringColor(hexString: "#BC87FF")], size: CGSize(width: 220, height: 40), radius: 22, locations: [0,1], direction: .horizontal) {
  48. btn.setBackgroundImage(image, for: .normal)
  49. }
  50. btn.setTitle("保存", for: .normal)
  51. btn.setTitleColor(.white, for: .normal)
  52. btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
  53. btn.addTarget(self, action: #selector(saveBtnClickAction), for: .touchUpInside)
  54. return btn
  55. }()
  56. override init(frame: CGRect) {
  57. super.init(frame: frame)
  58. initUI()
  59. }
  60. required init?(coder: NSCoder) {
  61. fatalError("init(coder:) has not been implemented")
  62. }
  63. }
  64. extension KeyboardExchangeView {
  65. @objc func backBtnClickAction() {
  66. delegate?.exchangeViewBackClickAction()
  67. // self.removeFromSuperview()
  68. }
  69. @objc func saveBtnClickAction() {
  70. if let keyboardList = self.keyboardList {
  71. delegate?.exchangeViewSaveClickAction(keyboardList: keyboardList) {
  72. // self.removeFromSuperview()
  73. }
  74. }
  75. }
  76. }
  77. extension KeyboardExchangeView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  78. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  79. return self.keyboardList?.count ?? 0
  80. }
  81. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  82. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KeyboardExchangeCell.reuseIdentifier(), for: indexPath) as! KeyboardExchangeCell
  83. if let model = self.keyboardList?[indexPath.row] {
  84. cell.config(model: model)
  85. }
  86. return cell
  87. }
  88. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  89. for i in 0..<(self.keyboardList?.count ?? 0) {
  90. self.keyboardList?[i].isChoose = false
  91. }
  92. self.keyboardList?[indexPath.row].isChoose = true
  93. self.collectionView.reloadData()
  94. }
  95. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  96. return CGSize(width: UX.cellWidth, height: UX.cellHeight)
  97. }
  98. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  99. return 8
  100. }
  101. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  102. return 9
  103. }
  104. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  105. return CGSize(width: KeyboardConst.kb_kScreenW, height: 45)
  106. }
  107. }
  108. extension KeyboardExchangeView {
  109. func initUI() {
  110. self.addSubview(backBtn)
  111. backBtn.snp.makeConstraints { make in
  112. make.size.equalTo(CGSize(width: 32, height: 32))
  113. make.top.equalTo(15)
  114. make.left.equalTo(12)
  115. }
  116. self.addSubview(collectionView)
  117. collectionView.snp.makeConstraints { make in
  118. make.left.equalTo(12)
  119. make.right.equalTo(-12)
  120. make.top.equalTo(backBtn.snp.bottom).offset(12)
  121. make.bottom.equalTo(0)
  122. }
  123. self.addSubview(saveBtn)
  124. saveBtn.snp.makeConstraints { make in
  125. make.size.equalTo(CGSize(width: 220, height: 40))
  126. make.centerX.equalToSuperview()
  127. make.bottom.equalToSuperview().offset(-12)
  128. }
  129. }
  130. }