123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- //
- // TSPTPSelectStyleView.swift
- // AIEmoji
- //
- // Created by 100Years on 2025/4/7.
- //
- class TSPTPSelectStyleView : TSBaseView{
- var viewH: CGFloat = 110.0
- var dataArray: [TSGenerateStyleModel] = [TSGenerateStyleModel](){
- didSet{
- styleCollectionView.reloadData()
-
- if dataArray.count > 0 {
- // DispatchQueue.main.async {
- self.styleCollectionView.selectItem(at: self.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: 110)
- 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()
- }
- }
-
- 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
- self.styleCollectionView.selectItem(at: self.currentIndexPath, animated: true, scrollPosition: .centeredHorizontally)
- clickHandle?(indexPath,model)
- }
- }
- }
|