317 lines
9.3 KiB
Swift
317 lines
9.3 KiB
Swift
//
|
|
// VPDetailRecommandView.swift
|
|
// Veloria
|
|
//
|
|
// Created by 湖南秦九 on 2025/6/6.
|
|
//
|
|
|
|
import UIKit
|
|
import FSPagerView
|
|
|
|
class VPDetailRecommandView: HWPanModalContentView {
|
|
|
|
|
|
var dataArr: [VPShortModel] = [] {
|
|
didSet {
|
|
|
|
CATransaction.setCompletionBlock { [weak self] in
|
|
self?.setVideoTitle()
|
|
}
|
|
|
|
CATransaction.begin()
|
|
bannerView.reloadData()
|
|
CATransaction.commit()
|
|
|
|
}
|
|
}
|
|
|
|
var clickCloseButton: (() -> Void)?
|
|
var clickLookButton: ((_ model: VPShortModel) -> Void)?
|
|
|
|
private var currentCell: VPDetailRecommandBannerCell? {
|
|
didSet {
|
|
if oldValue == currentCell { return }
|
|
|
|
oldValue?.isCurrentPlayer = false
|
|
oldValue?.player.pause()
|
|
|
|
currentCell?.isCurrentPlayer = true
|
|
}
|
|
}
|
|
|
|
|
|
//MARK: UI属性
|
|
private lazy var bgView: UIView = {
|
|
let view = VPGradientView()
|
|
view.locations = [0, 0.5, 1]
|
|
view.colors = [UIColor.color045241().cgColor, UIColor.color000000().cgColor, UIColor.color000000().cgColor]
|
|
view.startPoint = .init(x: 0, y: 0)
|
|
view.endPoint = .init(x: 1, y: 1)
|
|
return view
|
|
}()
|
|
|
|
private lazy var closeButton: UIButton = {
|
|
let button = UIButton(type: .custom)
|
|
button.setImage(UIImage(named: "close_icon_01"), for: .normal)
|
|
button.addTarget(self, action: #selector(handleCloseButton), for: .touchUpInside)
|
|
return button
|
|
}()
|
|
|
|
private lazy var titleLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.font = .fontMedium(ofSize: 16)
|
|
label.textColor = .colorFFFFFF()
|
|
label.text = "veloria_detail_recommand_title".localized
|
|
return label
|
|
}()
|
|
|
|
private lazy var bannerView: FSPagerView = {
|
|
let view = FSPagerView()
|
|
view.transformer = FSPagerViewTransformer(type: .overlap)
|
|
view.transformer?.minimumScale = 0.8
|
|
view.decelerationDistance = FSPagerView.automaticDistance
|
|
view.itemSize = .init(width: 194, height: 258)
|
|
view.isInfinite = true
|
|
view.delegate = self
|
|
view.dataSource = self
|
|
view.register(VPDetailRecommandBannerCell.self, forCellWithReuseIdentifier: "cell")
|
|
return view
|
|
}()
|
|
|
|
private lazy var videoTitleLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.font = .fontRegular(ofSize: 14)
|
|
label.textColor = .colorFFFFFF()
|
|
label.textAlignment = .center
|
|
return label
|
|
}()
|
|
|
|
private lazy var videoCategoryView: UIButton = {
|
|
var config = UIButton.Configuration.plain()
|
|
config.contentInsets = .init(top: 0, leading: 5, bottom: 0, trailing: 5)
|
|
|
|
let view = UIButton(configuration: config)
|
|
view.isUserInteractionEnabled = false
|
|
view.backgroundColor = .colorFFFFFF(alpha: 0.1)
|
|
view.layer.cornerRadius = 3
|
|
view.layer.masksToBounds = true
|
|
view.configurationUpdateHandler = { [weak self] button in
|
|
guard let self = self else { return }
|
|
if self.dataArr.count <= self.bannerView.currentIndex { return }
|
|
|
|
let model = self.dataArr[self.bannerView.currentIndex]
|
|
let category = model.category?.last ?? ""
|
|
|
|
let string = AttributedString.createAttributedString(string: category, color: .colorAFAFAF(), font: .fontRegular(ofSize: 10))
|
|
|
|
button.configuration?.attributedTitle = string
|
|
}
|
|
|
|
return view
|
|
}()
|
|
|
|
private lazy var lookButton: UIButton = {
|
|
let button = VPGradientButton(type: .custom)
|
|
button.locations = [0, 1]
|
|
button.colors = [UIColor.color05CEA0(alpha: 0.3).cgColor, UIColor.color7C174F(alpha: 0.3).cgColor]
|
|
button.startPoint = .init(x: 0, y: 0.3)
|
|
button.endPoint = .init(x: 1, y: 0.8)
|
|
button.bt_setGradientBorder()
|
|
button.setTitle("veloria_watch_now".localized, for: .normal)
|
|
button.setTitleColor(.colorFFFFFF(), for: .normal)
|
|
button.titleLabel?.font = .fontMedium(ofSize: 14)
|
|
button.layer.cornerRadius = 24
|
|
button.layer.masksToBounds = true
|
|
button.addTarget(self, action: #selector(handleLookButton), for: .touchUpInside)
|
|
return button
|
|
}()
|
|
|
|
|
|
deinit {
|
|
vpLog(message: "销毁")
|
|
}
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
|
|
|
|
vp_setupUI()
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
override func layoutSubviews() {
|
|
super.layoutSubviews()
|
|
|
|
}
|
|
|
|
|
|
//MARK: HWPanModalPresentable
|
|
override func longFormHeight() -> PanModalHeight {
|
|
return PanModalHeightMake(.content, 473 + UIScreen.tabbarSafeBottomMargin)
|
|
}
|
|
|
|
override func showDragIndicator() -> Bool {
|
|
return false
|
|
}
|
|
|
|
override func backgroundConfig() -> HWBackgroundConfig {
|
|
let config = HWBackgroundConfig()
|
|
config.backgroundAlpha = 0.6
|
|
return config
|
|
}
|
|
|
|
override func cornerRadius() -> CGFloat {
|
|
return 24
|
|
}
|
|
|
|
override func allowsTapBackgroundToDismiss() -> Bool {
|
|
return false
|
|
}
|
|
|
|
override func allowsDragToDismiss() -> Bool {
|
|
return false
|
|
}
|
|
|
|
override func minVerticalVelocityToTriggerDismiss() -> CGFloat {
|
|
return 0
|
|
}
|
|
|
|
override func allowsPullDownWhenShortState() -> Bool {
|
|
return false
|
|
}
|
|
|
|
}
|
|
|
|
extension VPDetailRecommandView {
|
|
|
|
private func setVideoTitle() {
|
|
let index = self.bannerView.currentIndex
|
|
guard self.dataArr.count > index else { return }
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
|
|
guard let self = self else { return }
|
|
|
|
let cell = self.bannerView.cellForItem(at: index) as? VPDetailRecommandBannerCell
|
|
self.currentCell = cell
|
|
self.currentCell?.player.start()
|
|
}
|
|
|
|
|
|
let model = self.dataArr[index]
|
|
videoTitleLabel.text = model.name
|
|
|
|
if let category = model.category?.last, !category.isEmpty {
|
|
videoCategoryView.isHidden = false
|
|
videoCategoryView.setNeedsUpdateConfiguration()
|
|
} else {
|
|
videoCategoryView.isHidden = true
|
|
}
|
|
|
|
}
|
|
|
|
@objc private func handleCloseButton() {
|
|
self.clickCloseButton?()
|
|
|
|
self.dismiss(animated: true) {
|
|
|
|
}
|
|
}
|
|
|
|
@objc private func handleLookButton() {
|
|
let index = self.bannerView.currentIndex
|
|
self.clickLookButton?(dataArr[index])
|
|
|
|
self.dismiss(animated: true) { }
|
|
}
|
|
|
|
|
|
}
|
|
|
|
extension VPDetailRecommandView {
|
|
|
|
private func vp_setupUI() {
|
|
addSubview(bgView)
|
|
addSubview(titleLabel)
|
|
addSubview(closeButton)
|
|
addSubview(bannerView)
|
|
addSubview(videoTitleLabel)
|
|
addSubview(videoCategoryView)
|
|
addSubview(lookButton)
|
|
|
|
bgView.snp.makeConstraints { make in
|
|
make.edges.equalToSuperview()
|
|
}
|
|
|
|
titleLabel.snp.makeConstraints { make in
|
|
make.centerX.equalToSuperview()
|
|
make.top.equalToSuperview().offset(39)
|
|
}
|
|
|
|
closeButton.snp.makeConstraints { make in
|
|
make.right.equalToSuperview().offset(-5)
|
|
make.top.equalToSuperview().offset(5)
|
|
make.width.height.equalTo(40)
|
|
}
|
|
|
|
bannerView.snp.makeConstraints { make in
|
|
make.left.right.equalToSuperview()
|
|
make.top.equalToSuperview().offset(77)
|
|
make.height.equalTo(260)
|
|
}
|
|
|
|
videoTitleLabel.snp.makeConstraints { make in
|
|
make.centerX.equalToSuperview()
|
|
make.top.equalTo(bannerView.snp.bottom).offset(9)
|
|
make.width.equalTo(200)
|
|
}
|
|
|
|
videoCategoryView.snp.makeConstraints { make in
|
|
// make.left.equalTo(videoTitleLabel)
|
|
make.centerX.equalToSuperview()
|
|
make.top.equalTo(bannerView.snp.bottom).offset(38)
|
|
make.height.equalTo(16)
|
|
}
|
|
|
|
lookButton.snp.makeConstraints { make in
|
|
make.centerX.equalToSuperview()
|
|
make.width.equalTo(240)
|
|
make.height.equalTo(48)
|
|
make.top.equalTo(bannerView.snp.bottom).offset(74)
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
//MARK: -------------- FSPagerViewDelegate FSPagerViewDataSource --------------
|
|
extension VPDetailRecommandView: FSPagerViewDelegate, FSPagerViewDataSource {
|
|
|
|
func numberOfItems(in pagerView: FSPagerView) -> Int {
|
|
return dataArr.count
|
|
}
|
|
|
|
func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
|
|
let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index) as! VPDetailRecommandBannerCell
|
|
cell.model = self.dataArr[index]
|
|
return cell
|
|
}
|
|
|
|
func pagerViewDidEndDecelerating(_ pagerView: FSPagerView) {
|
|
let newCell = pagerView.cellForItem(at: pagerView.currentIndex)
|
|
if newCell == currentCell {
|
|
return
|
|
}
|
|
self.currentCell?.isCurrentPlayer = false
|
|
self.currentCell?.player.pause()
|
|
|
|
setVideoTitle()
|
|
}
|
|
|
|
|
|
}
|
|
|