TSTTPStyleView.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // TSTTPStyleView.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/3/11.
  6. //
  7. class TSTTPStyleView:TSBaseView {
  8. var selectedValueBlock:((TSPTPStyleModel)->Void)?
  9. var dataArray: [TSPTPStyleModel] = [TSPTPStyleModel](){
  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: 100, height: 110)
  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(TSPromptStyleViewCell.self, forCellWithReuseIdentifier: TSPromptStyleViewCell.cellID)
  34. if #available(iOS 11.0, *) {
  35. collectionView.contentInsetAdjustmentBehavior = .never
  36. }
  37. return collectionView
  38. }()
  39. var currentIndexPath:IndexPath = IndexPath(item: 0, section: 0)
  40. lazy var titleView: TSTitleView = {
  41. let titleView = TSTitleView()
  42. titleView.titleLab.text = "Genre".localized
  43. return titleView
  44. }()
  45. override func creatUI() {
  46. let titleViewH = titleView.viewH
  47. contentView.addSubview(titleView)
  48. titleView.snp.makeConstraints { make in
  49. make.top.equalTo(0)
  50. make.leading.trailing.equalTo(0)
  51. make.height.equalTo(titleViewH)
  52. }
  53. currentIndexPath = IndexPath(item: 0, section: 0)
  54. contentView.addSubview(styleCollectionView)
  55. styleCollectionView.snp.makeConstraints { make in
  56. make.top.equalTo(titleView.snp.bottom)
  57. make.leading.trailing.equalTo(0)
  58. make.height.equalTo(110)
  59. make.bottom.equalTo(0)
  60. }
  61. }
  62. func unCheck(){
  63. styleCollectionView.deselectItem(at: currentIndexPath, animated: true)
  64. }
  65. }
  66. extension TSTTPStyleView: UICollectionViewDataSource ,UICollectionViewDelegate {
  67. public func numberOfSections(in collectionView: UICollectionView) -> Int {
  68. return 1
  69. }
  70. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  71. return dataArray.count
  72. }
  73. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  74. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TSPromptStyleViewCell.cellID, for: indexPath)
  75. if let cell = cell as? TSPromptStyleViewCell,let itemModel = dataArray.safeObj(At: indexPath.item){
  76. cell.itemModel = itemModel
  77. }
  78. return cell
  79. }
  80. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  81. if let model = dataArray.safeObj(At: indexPath.item){
  82. currentIndexPath = indexPath
  83. selectedValueBlock?(model)
  84. }
  85. }
  86. }
  87. class TSPromptStyleViewCell: TSBaseCollectionCell {
  88. static let cellID = "TSPromptStyleViewCell"
  89. override var isSelected: Bool{
  90. didSet{
  91. boardImageView.isHidden = isSelected ? false : true
  92. }
  93. }
  94. var itemModel:TSPTPStyleModel = TSPTPStyleModel(){
  95. didSet{
  96. imageView.image = UIImage(named: itemModel.imageName)
  97. if itemModel.style == 1 {
  98. hotImageView.isHidden = false
  99. // let font = UIFont.font(name:.PoppinsBlackItalic,size: 18)
  100. // textLabel.font = font
  101. // textLabel.attributedText = NSAttributedString(
  102. // string: itemModel.imageText,
  103. // attributes: [
  104. // .strokeColor: "#FFB73C".uiColor, // 描边颜色
  105. // .strokeWidth: -4.0, // 负值表示同时填充和描边(正值仅描边)
  106. // .foregroundColor: UIColor.white, // 文字填充色
  107. // .font: font
  108. // ]
  109. // )
  110. }else{
  111. hotImageView.isHidden = true
  112. // textLabel.font = .font(size: 14)
  113. // textLabel.attributedText = nil
  114. }
  115. textLabel.text = itemModel.imageText
  116. }
  117. }
  118. lazy var imageView: UIImageView = {
  119. let imageView = UIImageView()
  120. imageView.cornerRadius = 40.0
  121. return imageView
  122. }()
  123. lazy var hotImageView: UIImageView = {
  124. let hotImageView = UIImageView.createImageView(imageName: "ttp_style_hot")
  125. hotImageView.isHidden = true
  126. return hotImageView
  127. }()
  128. lazy var boardImageView: UIImageView = {
  129. let boardImageView = UIImageView.createImageView(imageName: "ttp_selected_border")
  130. boardImageView.isHidden = true
  131. return boardImageView
  132. }()
  133. lazy var textLabel: UILabel = {
  134. let textLabel = UILabel.createLabel(font: .font(size: 14),textColor: .white,textAlignment: .center,numberOfLines: 0)
  135. return textLabel
  136. }()
  137. override func creatUI() {
  138. //cell 100*110
  139. bgContentView.addSubview(imageView)
  140. imageView.snp.makeConstraints { make in
  141. make.top.equalTo(0)
  142. make.centerX.equalToSuperview()
  143. make.width.height.equalTo(80)
  144. }
  145. imageView.addSubview(hotImageView)
  146. hotImageView.snp.makeConstraints { make in
  147. make.leading.equalTo(0)
  148. make.top.equalTo(0.0)
  149. make.width.height.equalTo(36)
  150. }
  151. imageView.addSubview(boardImageView)
  152. boardImageView.snp.makeConstraints { make in
  153. make.top.bottom.leading.trailing.equalTo(0)
  154. }
  155. bgContentView.addSubview(textLabel)
  156. textLabel.snp.makeConstraints { make in
  157. make.top.equalTo(imageView.snp.bottom).offset(4)
  158. make.centerX.equalToSuperview()
  159. }
  160. }
  161. }