TSAIExpandStyleView.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // TSAIExpandStyleView.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/4/21.
  6. //
  7. class TSAIExpandStyleView: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: 52)
  21. layout.minimumInteritemSpacing = 0.0
  22. // layout.minimumLineSpacing = 0.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(TSAIExpandStyleCell.self, forCellWithReuseIdentifier: TSAIExpandStyleCell.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. UIView.performWithoutAnimation {
  50. self.styleCollectionView.reloadData()
  51. }
  52. }
  53. }
  54. override func creatUI() {
  55. currentIndexPath = IndexPath(item: 0, section: 0)
  56. contentView.addSubview(styleCollectionView)
  57. styleCollectionView.snp.makeConstraints { make in
  58. make.top.equalTo(0)
  59. make.leading.trailing.equalTo(0)
  60. make.height.equalTo(0)
  61. make.bottom.equalTo(0)
  62. }
  63. }
  64. func unCheck(){
  65. styleCollectionView.deselectItem(at: currentIndexPath, animated: true)
  66. }
  67. func agreeWillSelectIndexPath(){
  68. currentIndexPath = willSelectIndexPath
  69. }
  70. }
  71. extension TSAIExpandStyleView: UICollectionViewDataSource ,UICollectionViewDelegate {
  72. public func numberOfSections(in collectionView: UICollectionView) -> Int {
  73. return 1
  74. }
  75. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  76. return dataArray.count
  77. }
  78. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  79. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TSAIExpandStyleCell.cellID, for: indexPath)
  80. if let cell = cell as? TSAIExpandStyleCell,let itemModel = dataArray.safeObj(At: indexPath.item){
  81. cell.itemModel = itemModel
  82. }
  83. return cell
  84. }
  85. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  86. if let model = dataArray.safeObj(At: indexPath.item){
  87. willSelectIndexPath = indexPath
  88. selectedValueBlock?(model)
  89. }
  90. }
  91. }
  92. class TSAIExpandStyleCell: TSBaseCollectionCell {
  93. static let cellID = "TSAIExpandStyleCell"
  94. var itemModel:TSGenerateStyleModel = TSGenerateStyleModel(){
  95. didSet{
  96. if let image = UIImage(named: itemModel.imageName)?.withRenderingMode(.alwaysTemplate) {
  97. imageView.image = image
  98. }
  99. // vipImageView.isHidden = itemModel.isVip == false
  100. textLabel.text = itemModel.imageText.localized
  101. if itemModel.isSelected {
  102. textLabel.textColor = .themeColor
  103. imageView.tintColor = .themeColor
  104. }else{
  105. textLabel.textColor = .white
  106. imageView.tintColor = .white
  107. }
  108. }
  109. }
  110. lazy var imageView: UIImageView = {
  111. let imageView = UIImageView()
  112. return imageView
  113. }()
  114. // lazy var vipImageView: UIImageView = {
  115. // let vipImageView = UIImageView.createImageView(imageName: "ai_emo_style_vip")
  116. // vipImageView.isHidden = true
  117. // return vipImageView
  118. // }()
  119. lazy var textLabel: UILabel = {
  120. let textLabel = UILabel.createLabel(font: .font(size: 10,weight: .medium),textColor: .white,textAlignment: .center,numberOfLines: 2)
  121. textLabel.adjustsFontSizeToFitWidth = true
  122. textLabel.contentMode = .bottom
  123. return textLabel
  124. }()
  125. override func creatUI() {
  126. //cell 100*110
  127. bgContentView.addSubview(imageView)
  128. imageView.snp.makeConstraints { make in
  129. make.top.equalTo(0)
  130. make.centerX.equalToSuperview()
  131. make.width.equalTo(24)
  132. make.height.equalTo(24)
  133. }
  134. // imageView.addSubview(vipImageView)
  135. // vipImageView.snp.makeConstraints { make in
  136. // make.trailing.equalTo(0)
  137. // make.top.equalTo(0.0)
  138. // make.width.equalTo(18)
  139. // make.height.equalTo(14)
  140. // }
  141. bgContentView.addSubview(textLabel)
  142. textLabel.snp.makeConstraints { make in
  143. make.height.equalTo(16.0)
  144. // make.top.greaterThanOrEqualTo(imageView.snp.bottom).offset(2)
  145. // make.top.equalTo(imageView.snp.bottom).offset(2)
  146. make.bottom.equalTo(0)
  147. make.leading.trailing.equalToSuperview()
  148. }
  149. }
  150. }