TSPTPSelectStyleView.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // TSPTPSelectStyleView.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/4/7.
  6. //
  7. class TSPTPSelectStyleView : TSBaseView{
  8. var viewH: CGFloat = 110.0
  9. var dataArray: [TSGenerateStyleModel] = [TSGenerateStyleModel](){
  10. didSet{
  11. styleCollectionView.reloadData()
  12. if dataArray.count > 0 {
  13. // DispatchQueue.main.async {
  14. self.styleCollectionView.selectItem(at: self.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: 110)
  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. }
  50. }
  51. func unCheck(){
  52. styleCollectionView.deselectItem(at: currentIndexPath, animated: true)
  53. }
  54. }
  55. extension TSPTPSelectStyleView: UICollectionViewDataSource ,UICollectionViewDelegate {
  56. public func numberOfSections(in collectionView: UICollectionView) -> Int {
  57. return 1
  58. }
  59. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  60. return dataArray.count
  61. }
  62. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  63. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TSGennertatorSelectStyleCell.cellID, for: indexPath)
  64. if let cell = cell as? TSGennertatorSelectStyleCell,let itemModel = dataArray.safeObj(At: indexPath.item){
  65. cell.itemModel = itemModel
  66. }
  67. return cell
  68. }
  69. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  70. if let model = dataArray.safeObj(At: indexPath.item){
  71. currentIndexPath = indexPath
  72. self.styleCollectionView.selectItem(at: self.currentIndexPath, animated: true, scrollPosition: .centeredHorizontally)
  73. clickHandle?(indexPath,model)
  74. }
  75. }
  76. }