TSTTPStyleView.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // TSTTPStyleView.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/3/11.
  6. //
  7. class TSTTPStyleView:TSBaseView {
  8. var selectedValueBlock:((TSGenerateStyleModel)->Void)?
  9. var clickMoreBlock:(()->Void)?{
  10. didSet{
  11. titleView.clickMoreBlock = clickMoreBlock
  12. }
  13. }
  14. var dataArray: [TSGenerateStyleModel] = [TSGenerateStyleModel](){
  15. didSet{
  16. styleCollectionView.reloadData()
  17. if dataArray.count > 0 {
  18. self.styleCollectionView.selectItem(at: self.currentIndexPath, animated: false, scrollPosition: .centeredHorizontally)
  19. }
  20. }
  21. }
  22. lazy var layout: UICollectionViewFlowLayout = {
  23. let layout = UICollectionViewFlowLayout()
  24. layout.scrollDirection = .horizontal
  25. let w = (k_ScreenWidth-32.0-30.0-2.0)/4.0
  26. layout.itemSize = CGSize(width: w, height: 108)
  27. layout.minimumInteritemSpacing = 0.0
  28. layout.minimumLineSpacing = 0.0
  29. layout.sectionInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
  30. return layout
  31. }()
  32. lazy var styleCollectionView: UICollectionView = {
  33. let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  34. collectionView.delegate = self
  35. collectionView.dataSource = self
  36. collectionView.showsVerticalScrollIndicator = false
  37. collectionView.showsHorizontalScrollIndicator = false
  38. collectionView.backgroundColor = .clear
  39. collectionView.register(TSGennertatorSelectStyleCell.self, forCellWithReuseIdentifier: TSGennertatorSelectStyleCell.cellID)
  40. if #available(iOS 11.0, *) {
  41. collectionView.contentInsetAdjustmentBehavior = .never
  42. }
  43. return collectionView
  44. }()
  45. var currentIndexPath:IndexPath = IndexPath(item: 0, section: 0)
  46. lazy var titleView: TSTitleMoreView = {
  47. let titleView = TSTitleMoreView()
  48. titleView.setTitle(title: "Select Style".localized, clickMoreBlock: nil)
  49. return titleView
  50. }()
  51. override func creatUI() {
  52. let titleViewH = titleView.viewH
  53. contentView.addSubview(titleView)
  54. titleView.snp.makeConstraints { make in
  55. make.top.equalTo(0)
  56. make.leading.trailing.equalTo(0)
  57. make.height.equalTo(titleViewH)
  58. }
  59. currentIndexPath = IndexPath(item: 0, section: 0)
  60. contentView.addSubview(styleCollectionView)
  61. styleCollectionView.snp.makeConstraints { make in
  62. make.top.equalTo(titleView.snp.bottom)
  63. make.leading.trailing.equalTo(0)
  64. make.height.equalTo(110)
  65. make.bottom.equalTo(0)
  66. }
  67. }
  68. func unCheck(){
  69. styleCollectionView.deselectItem(at: currentIndexPath, animated: true)
  70. }
  71. }
  72. extension TSTTPStyleView: UICollectionViewDataSource ,UICollectionViewDelegate {
  73. public func numberOfSections(in collectionView: UICollectionView) -> Int {
  74. return 1
  75. }
  76. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  77. return dataArray.count
  78. }
  79. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  80. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TSGennertatorSelectStyleCell.cellID, for: indexPath)
  81. if let cell = cell as? TSGennertatorSelectStyleCell,let itemModel = dataArray.safeObj(At: indexPath.item){
  82. cell.style = .ttp
  83. cell.itemModel = itemModel
  84. }
  85. return cell
  86. }
  87. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  88. if let model = dataArray.safeObj(At: indexPath.item){
  89. currentIndexPath = indexPath
  90. self.styleCollectionView.selectItem(at: self.currentIndexPath, animated: true, scrollPosition: .centeredHorizontally)
  91. selectedValueBlock?(model)
  92. }
  93. }
  94. }
  95. class TSPromptStyleViewCell: TSBaseCollectionCell {
  96. static let cellID = "TSPromptStyleViewCell"
  97. override var isSelected: Bool{
  98. didSet{
  99. boardImageView.isHidden = isSelected ? false : true
  100. }
  101. }
  102. var itemModel:TSGenerateStyleModel = TSGenerateStyleModel(){
  103. didSet{
  104. imageView.image = UIImage(named: itemModel.imageName)
  105. if itemModel.specialStyle == 1 {
  106. hotImageView.isHidden = false
  107. }else{
  108. hotImageView.isHidden = true
  109. }
  110. textLabel.text = itemModel.imageText.localized
  111. }
  112. }
  113. lazy var imageView: UIImageView = {
  114. let imageView = UIImageView()
  115. imageView.cornerRadius = 40.0
  116. return imageView
  117. }()
  118. lazy var hotImageView: UIImageView = {
  119. let hotImageView = UIImageView.createImageView(imageName: "ttp_style_hot")
  120. hotImageView.isHidden = true
  121. return hotImageView
  122. }()
  123. lazy var boardImageView: UIImageView = {
  124. let boardImageView = UIImageView.createImageView(imageName: "ttp_selected_border")
  125. boardImageView.isHidden = true
  126. return boardImageView
  127. }()
  128. lazy var textLabel: UILabel = {
  129. let textLabel = UILabel.createLabel(font: .font(size: 14),textColor: .white,textAlignment: .center,numberOfLines: 0)
  130. return textLabel
  131. }()
  132. override func creatUI() {
  133. //cell 100*110
  134. bgContentView.addSubview(imageView)
  135. imageView.snp.makeConstraints { make in
  136. make.top.equalTo(0)
  137. make.centerX.equalToSuperview()
  138. make.width.height.equalTo(80)
  139. }
  140. imageView.addSubview(hotImageView)
  141. hotImageView.snp.makeConstraints { make in
  142. make.leading.equalTo(0)
  143. make.top.equalTo(0.0)
  144. make.width.height.equalTo(36)
  145. }
  146. imageView.addSubview(boardImageView)
  147. boardImageView.snp.makeConstraints { make in
  148. make.top.bottom.leading.trailing.equalTo(0)
  149. }
  150. bgContentView.addSubview(textLabel)
  151. textLabel.snp.makeConstraints { make in
  152. make.top.equalTo(imageView.snp.bottom).offset(4)
  153. make.centerX.equalToSuperview()
  154. }
  155. }
  156. }