TSPTPSelectStyleCell.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // TSPTPSelectStyleCell.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/2/25.
  6. //
  7. class TSPTPSelectStyleCell : TSBaseCollectionCell{
  8. var dataArray: [TSPTPStyleModel] = [TSPTPStyleModel](){
  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:TSPTPStyleModel = TSPTPStyleModel(){
  87. didSet{
  88. imageView.image = UIImage(named: itemModel.imageName)
  89. var strokeColor = UIColor.white
  90. var font = UIFont.font(size: 14)
  91. if itemModel.style == 1 {
  92. hotImageView.isHidden = false
  93. font = UIFont.font(name:.PoppinsBlackItalic,size: 18)
  94. strokeColor = "#FFB73C".uiColor
  95. }else{
  96. hotImageView.isHidden = true
  97. }
  98. textLabel.attributedText = NSAttributedString(
  99. string: itemModel.imageText,
  100. attributes: [
  101. .strokeColor: strokeColor, // 描边颜色
  102. .strokeWidth: -4.0, // 负值表示同时填充和描边(正值仅描边)
  103. .foregroundColor: UIColor.white, // 文字填充色
  104. .font: font
  105. ]
  106. )
  107. }
  108. }
  109. lazy var imageView: UIImageView = {
  110. let imageView = UIImageView()
  111. return imageView
  112. }()
  113. lazy var hotImageView: UIImageView = {
  114. let hotImageView = UIImageView.createImageView(imageName: "ptp_style_hot")
  115. hotImageView.isHidden = true
  116. return hotImageView
  117. }()
  118. lazy var boardImageView: UIImageView = {
  119. let boardImageView = UIImageView.createImageView(imageName: "ptp_selected_border")
  120. boardImageView.isHidden = true
  121. return boardImageView
  122. }()
  123. lazy var textLabel: UILabel = {
  124. let textLabel = UILabel.createLabel(font: .font(size: 14),textColor: .white,textAlignment: .center,numberOfLines: 0)
  125. return textLabel
  126. }()
  127. override func creatUI() {
  128. bgContentView.addSubview(imageView)
  129. imageView.snp.makeConstraints { make in
  130. make.top.bottom.leading.trailing.equalTo(0)
  131. }
  132. imageView.addSubview(hotImageView)
  133. hotImageView.snp.makeConstraints { make in
  134. make.top.leading.equalTo(0)
  135. make.width.height.equalTo(36)
  136. }
  137. bgContentView.addSubview(boardImageView)
  138. boardImageView.snp.makeConstraints { make in
  139. make.top.bottom.leading.trailing.equalTo(0)
  140. }
  141. bgContentView.addSubview(textLabel)
  142. textLabel.snp.makeConstraints { make in
  143. make.leading.trailing.equalTo(0)
  144. make.bottom.equalTo(-4)
  145. make.height.equalTo(37)
  146. }
  147. }
  148. }