TSPTPSelectStyleCell.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // TSPTPSelectStyleCell.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/2/25.
  6. //
  7. class TSPTPSelectStyleCell : TSBaseCollectionCell{
  8. var dataArray: [TSGenerateStyleModel] = [TSGenerateStyleModel](){
  9. didSet{
  10. styleCollectionView.reloadData()
  11. }
  12. }
  13. lazy var layout: UICollectionViewFlowLayout = {
  14. let layout = UICollectionViewFlowLayout()
  15. layout.scrollDirection = .horizontal
  16. layout.itemSize = CGSize(width: 110, height: 110)
  17. layout.minimumInteritemSpacing = 0.0
  18. layout.minimumLineSpacing = 12.0
  19. layout.sectionInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
  20. return layout
  21. }()
  22. lazy var styleCollectionView: UICollectionView = {
  23. let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  24. collectionView.delegate = self
  25. collectionView.dataSource = self
  26. collectionView.showsVerticalScrollIndicator = false
  27. collectionView.showsHorizontalScrollIndicator = false
  28. collectionView.backgroundColor = .clear
  29. collectionView.register(TSPTPSelectStyleCCell.self, forCellWithReuseIdentifier: TSPTPSelectStyleCCell.cellID)
  30. if #available(iOS 11.0, *) {
  31. collectionView.contentInsetAdjustmentBehavior = .never
  32. }
  33. return collectionView
  34. }()
  35. var currentIndexPath:IndexPath = IndexPath(item: 0, section: 0)
  36. override func creatUI() {
  37. currentIndexPath = IndexPath(item: 0, section: 0)
  38. addSubview(styleCollectionView)
  39. styleCollectionView.snp.makeConstraints { make in
  40. make.edges.equalToSuperview()
  41. }
  42. }
  43. func unCheck(){
  44. styleCollectionView.deselectItem(at: currentIndexPath, animated: true)
  45. }
  46. override func renderView(with object: Any?, component: TSCollectionViewComponent, attributes: [String : Any]?) {
  47. super.renderView(with: object, component: component, attributes: attributes)
  48. if let itemModel = object as? TSGenmojiCoLItemModel{
  49. dataArray = itemModel.ptpStyleModels
  50. if self.dataArray.count > 0 {
  51. DispatchQueue.main.async {
  52. self.styleCollectionView.selectItem(at: self.currentIndexPath, animated: true, scrollPosition: .centeredHorizontally)
  53. }
  54. }
  55. }
  56. }
  57. }
  58. extension TSPTPSelectStyleCell: UICollectionViewDataSource ,UICollectionViewDelegate {
  59. public func numberOfSections(in collectionView: UICollectionView) -> Int {
  60. return 1
  61. }
  62. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  63. return dataArray.count
  64. }
  65. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  66. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TSPTPSelectStyleCCell.cellID, for: indexPath)
  67. if let cell = cell as? TSPTPSelectStyleCCell,let itemModel = dataArray.safeObj(At: indexPath.item){
  68. cell.itemModel = itemModel
  69. }
  70. return cell
  71. }
  72. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  73. if let model = dataArray.safeObj(At: indexPath.item){
  74. currentIndexPath = indexPath
  75. actionHandler(any: model)
  76. }
  77. }
  78. }
  79. class TSPTPSelectStyleCCell: TSBaseCollectionCell {
  80. static let cellID = "TSPTPSelectStyleCCell"
  81. override var isSelected: Bool{
  82. didSet{
  83. boardImageView.isHidden = isSelected ? false : true
  84. }
  85. }
  86. var itemModel:TSGenerateStyleModel = TSGenerateStyleModel(){
  87. didSet{
  88. imageView.image = UIImage(named: itemModel.imageName)
  89. let hotImageNamed = getHotImageNamed(specialStyle: itemModel.specialStyle)
  90. if hotImageNamed.count > 0 {
  91. hotImageView.image = UIImage(named: hotImageNamed)
  92. hotImageView.isHidden = false
  93. }else{
  94. hotImageView.isHidden = true
  95. }
  96. // var strokeColor = UIColor.white
  97. // var font = UIFont.font(size: 14)
  98. // if itemModel.specialStyle == 1 {
  99. // font = UIFont.font(name:.PoppinsBlackItalic,size: 18)
  100. // strokeColor = "#FFB73C".uiColor
  101. // }
  102. //
  103. // textLabel.attributedText = NSAttributedString(
  104. // string: itemModel.imageText.localized,
  105. // attributes: [
  106. // .strokeColor: strokeColor, // 描边颜色
  107. // .strokeWidth: -4.0, // 负值表示同时填充和描边(正值仅描边)
  108. // .foregroundColor: UIColor.white, // 文字填充色
  109. // .font: font
  110. // ]
  111. // )
  112. textLabel.text = itemModel.imageText.localized
  113. }
  114. }
  115. func getHotImageNamed(specialStyle:Int)->String{
  116. if specialStyle == 1 {
  117. return "ptp_style_hot"
  118. }else if specialStyle == 2 {
  119. return "ptp_style_new"
  120. }else if specialStyle == 3 {
  121. return "ptp_style_max"
  122. }
  123. return ""
  124. }
  125. lazy var imageView: UIImageView = {
  126. let imageView = UIImageView()
  127. return imageView
  128. }()
  129. lazy var hotImageView: UIImageView = {
  130. let hotImageView = UIImageView.createImageView(imageName: "ptp_style_hot")
  131. hotImageView.isHidden = true
  132. return hotImageView
  133. }()
  134. lazy var boardImageView: UIImageView = {
  135. let boardImageView = UIImageView.createImageView(imageName: "ptp_selected_border")
  136. boardImageView.isHidden = true
  137. return boardImageView
  138. }()
  139. lazy var textLabel: UILabel = {
  140. let textLabel = UILabel.createLabel(font: .font(size: 14),textColor: .white,textAlignment: .center,numberOfLines: 0)
  141. textLabel.addShadow(shadowColor: UIColor.black.cgColor, shadowOffset: CGSize(width: 0, height: 2), shadowRadius: 2, shadowOpacity: 0.5)
  142. return textLabel
  143. }()
  144. override func creatUI() {
  145. bgContentView.addSubview(imageView)
  146. imageView.snp.makeConstraints { make in
  147. make.top.bottom.leading.trailing.equalTo(0)
  148. }
  149. imageView.addSubview(hotImageView)
  150. hotImageView.snp.makeConstraints { make in
  151. make.top.leading.equalTo(0)
  152. make.width.height.equalTo(36)
  153. }
  154. bgContentView.addSubview(boardImageView)
  155. boardImageView.snp.makeConstraints { make in
  156. make.top.bottom.leading.trailing.equalTo(0)
  157. }
  158. bgContentView.addSubview(textLabel)
  159. textLabel.snp.makeConstraints { make in
  160. make.leading.trailing.equalTo(0)
  161. make.bottom.equalTo(-4)
  162. make.height.equalTo(37)
  163. }
  164. }
  165. }