TSDiscoverStyleMoreCell.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // TSDiscoverStyleMoreCell.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/6/23.
  6. //
  7. class TSDiscoverStyleMoreCell : TSDiscoverBaseCell {
  8. lazy var collectionView: UICollectionView = {
  9. let layout = UICollectionViewFlowLayout()
  10. layout.scrollDirection = .horizontal
  11. layout.minimumLineSpacing = 16
  12. layout.minimumInteritemSpacing = 16
  13. layout.itemSize = CGSize(width: 150, height: 200)
  14. let collectionView = TSBaseCollectionView(frame: .zero, collectionViewLayout: layout)
  15. collectionView.delegate = self
  16. collectionView.dataSource = self
  17. collectionView.register(TSDiscoverStyleMoreBaseCell.self, forCellWithReuseIdentifier: "TSDiscoverStyleMoreBaseCell")
  18. collectionView.register(TSDiscoverStyleMoreAnimationCellCell.self, forCellWithReuseIdentifier: "TSDiscoverStyleMoreAnimationCellCell")
  19. collectionView.register(TSDiscoverStyleMoreAnimationGifCell.self, forCellWithReuseIdentifier: "TSDiscoverStyleMoreAnimationGifCell")
  20. collectionView.contentInset = kDiscoverSection
  21. return collectionView
  22. }()
  23. var models:[TSDiscoverItemModel] = []
  24. override func upDateModel() {
  25. if let models = items as? [TSDiscoverItemModel] {
  26. self.models = models
  27. }
  28. collectionView.reloadData()
  29. }
  30. override func creatUI() {
  31. bgContentView.addSubview(collectionView)
  32. collectionView.snp.makeConstraints { make in
  33. make.edges.equalToSuperview()
  34. }
  35. }
  36. }
  37. extension TSDiscoverStyleMoreCell: UICollectionViewDataSource ,UICollectionViewDelegate {
  38. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  39. return models.count
  40. }
  41. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  42. if let dbModel = models.safeObj(At: indexPath.row) {
  43. if let animationModel = dbModel.viewModel as? TSDiscoverAnimationItemVM {
  44. if animationModel.style == .comparison {
  45. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TSDiscoverStyleMoreAnimationCellCell", for: indexPath) as? TSDiscoverStyleMoreAnimationCellCell{
  46. cell.model = dbModel
  47. return cell
  48. }
  49. }else if animationModel.style == .gif {
  50. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TSDiscoverStyleMoreAnimationGifCell", for: indexPath) as? TSDiscoverStyleMoreAnimationGifCell{
  51. cell.model = dbModel
  52. return cell
  53. }
  54. }
  55. }else{
  56. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TSDiscoverStyleMoreBaseCell", for: indexPath) as? TSDiscoverStyleMoreBaseCell{
  57. cell.model = dbModel
  58. return cell
  59. }
  60. }
  61. }
  62. return UICollectionViewCell()
  63. }
  64. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  65. if let model = models.safeObj(At: indexPath.row) {
  66. clickBlock?(model)
  67. }
  68. }
  69. }
  70. private let CellCornerRadius = 16.0
  71. class TSDiscoverStyleMoreBaseCell: TSBaseCollectionCell {
  72. var model:TSDiscoverItemModel? {
  73. didSet {
  74. guard let model = model else { return }
  75. imageView.image = UIImage(named: model.viewModel.imageNamed)
  76. textLabel.text = model.name
  77. }
  78. }
  79. lazy var imageView: UIImageView = {
  80. let imageView = UIImageView.createImageView(contentMode: .scaleToFill,backgroundColor: .white.withAlphaComponent(0.2))
  81. imageView.cornerRadius = CellCornerRadius
  82. return imageView
  83. }()
  84. lazy var shadowView: UIImageView = {
  85. let imageView = UIImageView.createImageView(image:.discoverSmallShaow,contentMode: .scaleToFill)
  86. return imageView
  87. }()
  88. lazy var textLabel: UILabel = {
  89. let textLabel = UILabel.createLabel(font: .font(size: 14,weight: .medium),textColor: .white)
  90. return textLabel
  91. }()
  92. override func creatUI() {
  93. bgContentView.addSubview(imageView)
  94. bgContentView.addSubview(shadowView)
  95. bgContentView.addSubview(textLabel)
  96. imageView.snp.makeConstraints { make in
  97. make.edges.equalToSuperview()
  98. }
  99. shadowView.snp.makeConstraints { make in
  100. make.height.equalTo(40)
  101. make.leading.trailing.bottom.equalToSuperview()
  102. }
  103. textLabel.snp.makeConstraints { make in
  104. make.leading.equalTo(8)
  105. make.trailing.bottom.equalTo(-8)
  106. }
  107. }
  108. }
  109. class TSDiscoverStyleMoreAnimationCellCell: TSDiscoverStyleMoreBaseCell {
  110. override var model:TSDiscoverItemModel? {
  111. didSet {
  112. guard let model = model else { return }
  113. textLabel.text = model.name
  114. if let animationModel = model.viewModel as? TSDiscoverAnimationItemVM {
  115. var images = [UIImage]()
  116. for imageNamed in animationModel.imageNameds {
  117. if let image = UIImage(named: imageNamed) {
  118. images.append(image)
  119. }
  120. }
  121. comparisonView.images = images
  122. // comparisonView.startAnimation(direction: .rightToLeft)
  123. }
  124. }
  125. }
  126. lazy var comparisonView: TSImagesComparisonView = {
  127. let comparisonView = TSImagesComparisonView(frame: CGRect(x: 0, y: 300, width: 300, height: 400))
  128. comparisonView.isRunloop = true
  129. comparisonView.cornerRadius = CellCornerRadius
  130. return comparisonView
  131. }()
  132. override func creatUI() {
  133. super.creatUI()
  134. imageView.addSubview(comparisonView)
  135. comparisonView.snp.makeConstraints { make in
  136. make.edges.equalToSuperview()
  137. }
  138. kMainAsync {
  139. self.comparisonView.startAnimation(direction: .rightToLeft)
  140. }
  141. }
  142. }
  143. import Kingfisher
  144. class TSDiscoverStyleMoreAnimationGifCell: TSDiscoverStyleMoreBaseCell {
  145. override var model:TSDiscoverItemModel? {
  146. didSet {
  147. guard let model = model else { return }
  148. textLabel.text = model.name
  149. if let animationModel = model.viewModel as? TSDiscoverAnimationItemVM,let imageNamed = animationModel.imageNameds.first {
  150. if let gifURL = Bundle.main.url(forResource: imageNamed, withExtension: "gif") {
  151. animatedImageView.kf.setImage(with: gifURL, options: [.cacheOriginalImage]){ result in
  152. switch result {
  153. case .success(let _):
  154. self.animatedImageView.startAnimating()
  155. case .failure(let error):
  156. print("GIF 加载失败: \(error.localizedDescription)")
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. lazy var animatedImageView: AnimatedImageView = {
  164. let animatedImageView = AnimatedImageView()
  165. animatedImageView.autoPlayAnimatedImage = false
  166. return animatedImageView
  167. }()
  168. override func creatUI() {
  169. super.creatUI()
  170. imageView.addSubview(animatedImageView)
  171. animatedImageView.snp.makeConstraints { make in
  172. make.edges.equalToSuperview()
  173. }
  174. }
  175. }