12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- //
- // TSPTPSelectStyleView.swift
- // AIEmoji
- //
- // Created by 100Years on 2025/4/7.
- //
- class TSPTPSelectStyleView: TSBaseView {
- var viewH: CGFloat = 108.0
- var dataArray: [TSGenerateStyleModel] = [TSGenerateStyleModel]() {
- didSet {
- styleCollectionView.reloadData()
- if dataArray.count > 0 {
- // DispatchQueue.main.async {
- styleCollectionView.selectItem(at: currentIndexPath, animated: false, scrollPosition: .centeredHorizontally)
- // }
- }
- }
- }
- var clickHandle: ((IndexPath, TSGenerateStyleModel) -> Void)?
- lazy var layout: UICollectionViewFlowLayout = {
- let layout = UICollectionViewFlowLayout()
- layout.scrollDirection = .horizontal
- let w = (k_ScreenWidth - 32.0 - 30.0 - 2.0) / 4.0
- layout.itemSize = CGSize(width: w, height: viewH)
- layout.minimumInteritemSpacing = 0.0
- layout.minimumLineSpacing = 10.0
- layout.sectionInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
- return layout
- }()
- lazy var styleCollectionView: UICollectionView = {
- let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
- collectionView.delegate = self
- collectionView.dataSource = self
- collectionView.showsVerticalScrollIndicator = false
- collectionView.showsHorizontalScrollIndicator = false
- collectionView.backgroundColor = .clear
- collectionView.register(TSGennertatorSelectStyleCell.self, forCellWithReuseIdentifier: TSGennertatorSelectStyleCell.cellID)
- if #available(iOS 11.0, *) {
- collectionView.contentInsetAdjustmentBehavior = .never
- }
- return collectionView
- }()
- var currentIndexPath: IndexPath = IndexPath(item: 0, section: 0)
- override func creatUI() {
- currentIndexPath = IndexPath(item: 0, section: 0)
- addSubview(styleCollectionView)
- styleCollectionView.snp.makeConstraints { make in
- make.edges.equalToSuperview()
- make.height.equalTo(viewH)
- }
- }
- func selectStyle(styleId: String) {
- let index = dataArray.firstIndex {
- $0.styleId == styleId
- } ?? 0
- let indexPath = IndexPath(item: index, section: 0)
- collectionView(styleCollectionView, didSelectItemAt: indexPath)
- }
- func unCheck() {
- styleCollectionView.deselectItem(at: currentIndexPath, animated: true)
- }
- }
- extension TSPTPSelectStyleView: UICollectionViewDataSource, UICollectionViewDelegate {
- public func numberOfSections(in collectionView: UICollectionView) -> Int {
- return 1
- }
- public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return dataArray.count
- }
- public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TSGennertatorSelectStyleCell.cellID, for: indexPath)
- if let cell = cell as? TSGennertatorSelectStyleCell, let itemModel = dataArray.safeObj(At: indexPath.item) {
- cell.itemModel = itemModel
- }
- return cell
- }
- public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- if let model = dataArray.safeObj(At: indexPath.item) {
- currentIndexPath = indexPath
- styleCollectionView.selectItem(at: currentIndexPath, animated: true, scrollPosition: .centeredHorizontally)
- clickHandle?(indexPath, model)
- }
- }
- }
|