TSPTPSelectStyleView.swift 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // TSPTPSelectStyleView.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/4/7.
  6. //
  7. class TSPTPSelectStyleView: TSBaseView {
  8. var viewH: CGFloat = 108.0
  9. var dataArray: [TSGenerateStyleModel] = [TSGenerateStyleModel]() {
  10. didSet {
  11. styleCollectionView.reloadData()
  12. if dataArray.count > 0 {
  13. // DispatchQueue.main.async {
  14. styleCollectionView.selectItem(at: currentIndexPath, animated: false, scrollPosition: .centeredHorizontally)
  15. // }
  16. }
  17. }
  18. }
  19. var clickHandle: ((IndexPath, TSGenerateStyleModel) -> Void)?
  20. lazy var layout: UICollectionViewFlowLayout = {
  21. let layout = UICollectionViewFlowLayout()
  22. layout.scrollDirection = .horizontal
  23. let w = (k_ScreenWidth - 32.0 - 30.0 - 2.0) / 4.0
  24. layout.itemSize = CGSize(width: w, height: viewH)
  25. layout.minimumInteritemSpacing = 0.0
  26. layout.minimumLineSpacing = 10.0
  27. layout.sectionInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
  28. return layout
  29. }()
  30. lazy var styleCollectionView: UICollectionView = {
  31. let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  32. collectionView.delegate = self
  33. collectionView.dataSource = self
  34. collectionView.showsVerticalScrollIndicator = false
  35. collectionView.showsHorizontalScrollIndicator = false
  36. collectionView.backgroundColor = .clear
  37. collectionView.register(TSGennertatorSelectStyleCell.self, forCellWithReuseIdentifier: TSGennertatorSelectStyleCell.cellID)
  38. if #available(iOS 11.0, *) {
  39. collectionView.contentInsetAdjustmentBehavior = .never
  40. }
  41. return collectionView
  42. }()
  43. var currentIndexPath: IndexPath = IndexPath(item: 0, section: 0)
  44. override func creatUI() {
  45. currentIndexPath = IndexPath(item: 0, section: 0)
  46. addSubview(styleCollectionView)
  47. styleCollectionView.snp.makeConstraints { make in
  48. make.edges.equalToSuperview()
  49. make.height.equalTo(viewH)
  50. }
  51. }
  52. func selectStyle(styleId: String) {
  53. let index = dataArray.firstIndex {
  54. $0.styleId == styleId
  55. } ?? 0
  56. let indexPath = IndexPath(item: index, section: 0)
  57. collectionView(styleCollectionView, didSelectItemAt: indexPath)
  58. }
  59. func unCheck() {
  60. styleCollectionView.deselectItem(at: currentIndexPath, animated: true)
  61. }
  62. }
  63. extension TSPTPSelectStyleView: UICollectionViewDataSource, UICollectionViewDelegate {
  64. public func numberOfSections(in collectionView: UICollectionView) -> Int {
  65. return 1
  66. }
  67. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  68. return dataArray.count
  69. }
  70. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  71. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TSGennertatorSelectStyleCell.cellID, for: indexPath)
  72. if let cell = cell as? TSGennertatorSelectStyleCell, let itemModel = dataArray.safeObj(At: indexPath.item) {
  73. cell.itemModel = itemModel
  74. }
  75. return cell
  76. }
  77. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  78. if let model = dataArray.safeObj(At: indexPath.item) {
  79. currentIndexPath = indexPath
  80. styleCollectionView.selectItem(at: currentIndexPath, animated: true, scrollPosition: .centeredHorizontally)
  81. clickHandle?(indexPath, model)
  82. }
  83. }
  84. }