TSAIChangeEmoteStyleView.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // TSAIChangeEmoteStyleView.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/4/10.
  6. //
  7. class TSAIChangeEmoteStyleView:TSBaseView {
  8. var selectedValueBlock:((TSGenerateStyleModel)->Void)?
  9. var dataArray: [TSGenerateStyleModel] = [TSGenerateStyleModel](){
  10. didSet{
  11. styleCollectionView.reloadData()
  12. if dataArray.count > 0 {
  13. self.styleCollectionView.selectItem(at: self.currentIndexPath, animated: false, scrollPosition: .centeredHorizontally)
  14. }
  15. }
  16. }
  17. lazy var layout: UICollectionViewFlowLayout = {
  18. let layout = UICollectionViewFlowLayout()
  19. layout.scrollDirection = .horizontal
  20. layout.itemSize = CGSize(width: 60, height: 100)
  21. layout.minimumInteritemSpacing = 12.0
  22. // layout.minimumLineSpacing = 12.0
  23. layout.sectionInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
  24. return layout
  25. }()
  26. lazy var styleCollectionView: UICollectionView = {
  27. let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  28. collectionView.delegate = self
  29. collectionView.dataSource = self
  30. collectionView.showsVerticalScrollIndicator = false
  31. collectionView.showsHorizontalScrollIndicator = false
  32. collectionView.backgroundColor = .clear
  33. collectionView.register(TSAIChangeEmoteStyleCell.self, forCellWithReuseIdentifier: TSAIChangeEmoteStyleCell.cellID)
  34. if #available(iOS 11.0, *) {
  35. collectionView.contentInsetAdjustmentBehavior = .never
  36. }
  37. return collectionView
  38. }()
  39. lazy var willSelectIndexPath = currentIndexPath
  40. var currentIndexPath:IndexPath = IndexPath(item: 0, section: 0){
  41. didSet{
  42. for (i,model) in dataArray.enumerated(){
  43. if i == currentIndexPath.item {
  44. model.isSelected = true
  45. }else {
  46. model.isSelected = false
  47. }
  48. }
  49. styleCollectionView.reloadData()
  50. }
  51. }
  52. override func creatUI() {
  53. currentIndexPath = IndexPath(item: 0, section: 0)
  54. contentView.addSubview(styleCollectionView)
  55. styleCollectionView.snp.makeConstraints { make in
  56. make.top.equalTo(0)
  57. make.leading.trailing.equalTo(0)
  58. make.height.equalTo(0)
  59. make.bottom.equalTo(0)
  60. }
  61. }
  62. func unCheck(){
  63. styleCollectionView.deselectItem(at: currentIndexPath, animated: true)
  64. }
  65. func agreeWillSelectIndexPath(){
  66. currentIndexPath = willSelectIndexPath
  67. }
  68. }
  69. extension TSAIChangeEmoteStyleView: UICollectionViewDataSource ,UICollectionViewDelegate {
  70. public func numberOfSections(in collectionView: UICollectionView) -> Int {
  71. return 1
  72. }
  73. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  74. return dataArray.count
  75. }
  76. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  77. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TSAIChangeEmoteStyleCell.cellID, for: indexPath)
  78. if let cell = cell as? TSAIChangeEmoteStyleCell,let itemModel = dataArray.safeObj(At: indexPath.item){
  79. cell.itemModel = itemModel
  80. }
  81. return cell
  82. }
  83. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  84. if let model = dataArray.safeObj(At: indexPath.item){
  85. willSelectIndexPath = indexPath
  86. selectedValueBlock?(model)
  87. }
  88. }
  89. // // 在 didSelectItemAt 方法中处理选中
  90. // func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  91. //
  92. // if let model = dataArray.safeObj(At: indexPath.item),let selectedValueBlock = selectedValueBlock{
  93. // if selectedValueBlock(model){
  94. // currentIndexPath = indexPath
  95. // handleSelectedCellForItem(at: indexPath, isSelected: true)
  96. // }else{
  97. // handleSelectedCellForItem(at: indexPath, isSelected: false)
  98. // }
  99. // }
  100. //
  101. //
  102. // }
  103. //
  104. //// func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
  105. //// if let cell = collectionView.cellForItem(at: indexPath) {
  106. //// cell.isSelected = false
  107. //// // 可以在这里更新 UI 以反映取消选中状态
  108. //// }
  109. //// }
  110. //
  111. // func handleSelectedCellForItem(at indexPath: IndexPath,isSelected:Bool){
  112. // if let cell = styleCollectionView.cellForItem(at: indexPath) {
  113. // cell.isSelected = true
  114. // }
  115. // }
  116. }
  117. class TSAIChangeEmoteStyleCell: TSBaseCollectionCell {
  118. static let cellID = "TSAIChangeEmoteStyleCell"
  119. // override var isSelected: Bool{
  120. // didSet{
  121. // boardImageView.isHidden = isSelected ? false : true
  122. // }
  123. // }
  124. var itemModel:TSGenerateStyleModel = TSGenerateStyleModel(){
  125. didSet{
  126. imageView.image = UIImage(named: itemModel.imageName)
  127. vipImageView.isHidden = itemModel.isVip == false
  128. textLabel.text = itemModel.imageText.localized
  129. boardImageView.isHidden = itemModel.isSelected ? false : true
  130. }
  131. }
  132. lazy var imageView: UIImageView = {
  133. let imageView = UIImageView()
  134. imageView.cornerRadius = 8.0
  135. return imageView
  136. }()
  137. lazy var vipImageView: UIImageView = {
  138. let vipImageView = UIImageView.createImageView(imageName: "ai_emo_style_vip")
  139. vipImageView.isHidden = true
  140. return vipImageView
  141. }()
  142. lazy var boardImageView: UIImageView = {
  143. let boardImageView = UIImageView.createImageView(imageName: "ai_emo_selected_border",contentMode: .scaleToFill)
  144. boardImageView.isHidden = true
  145. return boardImageView
  146. }()
  147. lazy var textLabel: UILabel = {
  148. let textLabel = UILabel.createLabel(font: .font(size: 10,weight: .medium),textColor: .white,textAlignment: .center,numberOfLines: 0)
  149. return textLabel
  150. }()
  151. override func creatUI() {
  152. //cell 100*110
  153. bgContentView.addSubview(imageView)
  154. imageView.snp.makeConstraints { make in
  155. make.top.equalTo(0)
  156. make.centerX.equalToSuperview()
  157. make.width.equalTo(60)
  158. make.height.equalTo(80)
  159. }
  160. imageView.addSubview(vipImageView)
  161. vipImageView.snp.makeConstraints { make in
  162. make.trailing.equalTo(0)
  163. make.top.equalTo(0.0)
  164. make.width.equalTo(18)
  165. make.height.equalTo(14)
  166. }
  167. imageView.addSubview(boardImageView)
  168. boardImageView.snp.makeConstraints { make in
  169. make.top.bottom.leading.trailing.equalTo(0)
  170. }
  171. bgContentView.addSubview(textLabel)
  172. textLabel.snp.makeConstraints { make in
  173. // make.top.equalTo(imageView.snp.bottom).offset(2)
  174. make.bottom.equalTo(0)
  175. make.height.equalTo(16.0)
  176. make.leading.trailing.equalToSuperview()
  177. }
  178. }
  179. }