// // SPSpeedSelectedView.swift // ShortPlay // // Created by 曾觉新 on 2025/4/17. // import UIKit class SPSpeedSelectedView: UIView { override var intrinsicContentSize: CGSize { return CGSize(width: kSPScreenWidth, height: 54) } var didSelectedSpeed: ((_ model: SPSpeedModel) -> Void)? var currentSpeed = SPSpeedModel.Speed.x1 { didSet { self.collectionView.reloadData() } } private lazy var dataArr: [SPSpeedModel] = SPSpeedModel.getAllSpeed() //MARK: UI属性 private lazy var collectionViewLayout: UICollectionViewFlowLayout = { let layout = UICollectionViewFlowLayout() layout.scrollDirection = .horizontal layout.itemSize = .init(width: 70, height: 54) layout.minimumLineSpacing = 10 layout.minimumInteritemSpacing = 10 layout.sectionInset = .init(top: 0, left: 15, bottom: 0, right: 15) return layout }() private lazy var collectionView: SPCollectionView = { let collectionView = SPCollectionView(frame: .zero, collectionViewLayout: collectionViewLayout) collectionView.delegate = self collectionView.dataSource = self collectionView.showsVerticalScrollIndicator = false collectionView.showsHorizontalScrollIndicator = false SPSpeedSelectedCell.registerCell(collectionView: collectionView) return collectionView }() override init(frame: CGRect) { super.init(frame: frame) _setupUI() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } extension SPSpeedSelectedView { private func _setupUI() { addSubview(collectionView) collectionView.snp.makeConstraints { make in make.edges.equalToSuperview() } } } //MARK: -------------- UICollectionViewDelegate & UICollectionViewDataSource -------------- extension SPSpeedSelectedView: UICollectionViewDelegate, UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let model = dataArr[indexPath.row] let cell = SPSpeedSelectedCell.dequeueReusableCell(collectionView: collectionView, indexPath: indexPath) cell.model = model cell.sp_isSelected = model.speed == currentSpeed return cell } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return dataArr.count } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { self.didSelectedSpeed?(dataArr[indexPath.row]) } }