103 lines
3.3 KiB
Swift
103 lines
3.3 KiB
Swift
//
|
|
// VPRateSelectedView.swift
|
|
// Veloria
|
|
//
|
|
// Created by 湖南秦九 on 2025/5/23.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class VPRateSelectedView: UIView {
|
|
|
|
var currentRateModel: VPVideoRateModel? {
|
|
didSet {
|
|
collectionView.reloadData()
|
|
}
|
|
}
|
|
|
|
var didSelected: ((_ rateModel: VPVideoRateModel) -> Void)?
|
|
|
|
private lazy var rateArr: [VPVideoRateModel] = VPVideoRateModel.getAllRate()
|
|
|
|
private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
|
|
let layout = UICollectionViewFlowLayout()
|
|
layout.scrollDirection = .horizontal
|
|
layout.itemSize = .init(width: 70, height: 54)
|
|
layout.minimumLineSpacing = 10
|
|
layout.sectionInset = .init(top: 0, left: 15, bottom: 0, right: 15)
|
|
return layout
|
|
}()
|
|
|
|
private lazy var collectionView: VPCollectionView = {
|
|
let collectionView = VPCollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
|
|
collectionView.delegate = self
|
|
collectionView.dataSource = self
|
|
collectionView.register(VPRateSelectedCell.self, forCellWithReuseIdentifier: "cell")
|
|
collectionView.showsHorizontalScrollIndicator = false
|
|
return collectionView
|
|
}()
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
let tap = UITapGestureRecognizer(target: self, action: #selector(handleDismiss))
|
|
tap.delegate = self
|
|
self.addGestureRecognizer(tap)
|
|
vp_setupUI()
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
@objc func handleDismiss() {
|
|
self.removeFromSuperview()
|
|
}
|
|
}
|
|
|
|
extension VPRateSelectedView {
|
|
|
|
private func vp_setupUI() {
|
|
addSubview(collectionView)
|
|
|
|
collectionView.snp.makeConstraints { make in
|
|
make.left.right.equalToSuperview()
|
|
make.bottom.equalToSuperview().offset(-(UIScreen.tabbarSafeBottomMargin + 85))
|
|
make.height.equalTo(54)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
//MARK: -------------- UICollectionViewDelegate UICollectionViewDataSource --------------
|
|
extension VPRateSelectedView: UICollectionViewDelegate, UICollectionViewDataSource {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
let model = rateArr[indexPath.row]
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! VPRateSelectedCell
|
|
cell.model = model
|
|
cell.vp_isSelected = model.rate == currentRateModel?.rate
|
|
return cell
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
return rateArr.count
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
let model = rateArr[indexPath.row]
|
|
self.didSelected?(model)
|
|
self.handleDismiss()
|
|
}
|
|
}
|
|
|
|
//MARK: -------------- UIGestureRecognizerDelegate --------------
|
|
extension VPRateSelectedView: UIGestureRecognizerDelegate {
|
|
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
|
|
if touch.view != self {
|
|
return false
|
|
} else {
|
|
return true
|
|
}
|
|
}
|
|
}
|