// // SPHomeTrendingView.swift // MoviaBox // // Created by 曾觉新 on 2025/4/14. // import UIKit class SPHomeTrendingView: UIView { private static func itemSize() -> CGSize { return CGSize(width: 224, height: 106) } class func contentHeight(dataArr: [SPShortModel]) -> CGFloat { return 388 } var dataArr: [SPShortModel]? { didSet { self.collectionView.reloadData() } } //MARK: UI属性 private lazy var bgView: UIImageView = { let view = UIImageView(image: UIImage(named: "category_bg_image_03")) return view }() private lazy var titleLabel: UILabel = { let label = UILabel() label.font = .fontMedium(ofSize: 18) label.textColor = .colorFFC234() return label }() private lazy var collectionViewLayout: UICollectionViewFlowLayout = { let layout = UICollectionViewFlowLayout() layout.minimumLineSpacing = 12 layout.minimumInteritemSpacing = 12 layout.itemSize = Self.itemSize() layout.sectionInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16) layout.scrollDirection = .horizontal return layout }() private lazy var collectionView: SPCollectionView = { let collectionView = SPCollectionView(frame: .zero, collectionViewLayout: collectionViewLayout) collectionView.delegate = self collectionView.dataSource = self // collectionView.isScrollEnabled = false collectionView.showsVerticalScrollIndicator = false collectionView.showsHorizontalScrollIndicator = false SPHomeTrendingCell.registerCell(collectionView: collectionView) return collectionView }() override init(frame: CGRect) { super.init(frame: frame) self.titleLabel.text = "movia_trending_now".localized _setupUI() } @MainActor required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } extension SPHomeTrendingView { private func _setupUI() { addSubview(bgView) addSubview(titleLabel) addSubview(collectionView) titleLabel.snp.makeConstraints { make in make.left.equalToSuperview().offset(16) make.top.equalToSuperview().offset(14) } bgView.snp.makeConstraints { make in make.left.equalToSuperview() make.top.equalToSuperview() } collectionView.snp.makeConstraints { make in make.left.right.bottom.equalToSuperview() make.top.equalToSuperview().offset(46) } } } //MARK: -------------- UICollectionViewDataSource & UICollectionViewDelegate -------------- extension SPHomeTrendingView: UICollectionViewDataSource, UICollectionViewDelegate { func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = SPHomeTrendingCell.dequeueReusableCell(collectionView: collectionView, indexPath: indexPath) cell.row = indexPath.row cell.model = dataArr?[indexPath.row] return cell } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { var count = self.dataArr?.count ?? 0 if count > 9 { count = 9 } return count } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let model = self.dataArr?[indexPath.row] let vc = SPPlayerDetailViewController() vc.shortPlayId = model?.short_play_id self.viewController?.navigationController?.pushViewController(vc, animated: true) } }