TSTTPStyleView.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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: true, 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. textLabel.text = itemModel.imageText
  98. }
  99. }
  100. lazy var imageView: UIImageView = {
  101. let imageView = UIImageView()
  102. return imageView
  103. }()
  104. lazy var boardImageView: UIImageView = {
  105. let boardImageView = UIImageView.createImageView(imageName: "ttp_selected_border")
  106. boardImageView.isHidden = true
  107. return boardImageView
  108. }()
  109. lazy var textLabel: UILabel = {
  110. let textLabel = UILabel.createLabel(font: .font(size: 14),textColor: .white,textAlignment: .center,numberOfLines: 0)
  111. return textLabel
  112. }()
  113. override func creatUI() {
  114. //cell 100*110
  115. bgContentView.addSubview(imageView)
  116. imageView.snp.makeConstraints { make in
  117. make.top.equalTo(0)
  118. make.centerX.equalToSuperview()
  119. make.width.height.equalTo(80)
  120. }
  121. imageView.addSubview(boardImageView)
  122. boardImageView.snp.makeConstraints { make in
  123. make.top.bottom.leading.trailing.equalTo(0)
  124. }
  125. bgContentView.addSubview(textLabel)
  126. textLabel.snp.makeConstraints { make in
  127. make.top.equalTo(imageView.snp.bottom).offset(4)
  128. make.centerX.equalToSuperview()
  129. }
  130. }
  131. }