// // SPVipAlertView.swift // MoviaBox // // Created by 佳尔 on 2025/5/14. // import UIKit class SPVipAlertView: SPAlertView { ///上一次弹窗时间 static var lastAlertDate: Date? = UserDefaults.standard.object(forKey: kSPVipAlertDateDefaultsKey) as? Date { didSet { UserDefaults.standard.set(lastAlertDate, forKey: kSPVipAlertDateDefaultsKey) UserDefaults.standard.synchronize() } } static var isShowAlert: Bool { #if DEBUG return true #else guard let lastAlertDate = lastAlertDate else { return true } let nowDate = Date() let interval = nowDate.timeIntervalSince1970 - lastAlertDate.timeIntervalSince1970 //一个小时弹出一次 if interval > 60 * 60 { return true } else { return false } #endif } private lazy var dataArr: [SPPayTemplateItem] = [] ///会员购买成功 var buyFinishHandle: (() -> Void)? private var selectedIndex = 0 //MARK: UI属性 private lazy var bgImageView: UIImageView = { let imageView = UIImageView(image: UIImage(named: "alert_bg_image_02")) imageView.isUserInteractionEnabled = true return imageView }() private lazy var vipIconImageView: UIImageView = { let imageView = UIImageView(image: UIImage(named: "vip_icon_09")) return imageView }() // private lazy var vipTitleLabel: UILabel = { // let label = UILabel() // label.font = .fontMedium(ofSize: 16) // label.textColor = .colorFFC591() // label.text = "movia_vip_alert_text_01".localized // label.numberOfLines = 2 //// label.adjustsFontSizeToFitWidth = true // return label // }() private lazy var vipTipLabel1: UILabel = { let label = UILabel() label.font = .fontRegular(ofSize: 14) label.textColor = .colorBC7616() label.text = "movia_vip_alert_text_02".localized return label }() private lazy var vipTipLabel2: UILabel = { let label = UILabel() label.font = .fontRegular(ofSize: 12) label.textColor = .color9C9896() label.text = "movia_buy_menber_tip".localized return label }() private lazy var collectionViewLayout: UICollectionViewFlowLayout = { let layout = UICollectionViewFlowLayout() layout.scrollDirection = .vertical layout.itemSize = CGSize(width: 293, height: 65) layout.minimumLineSpacing = 7 return layout }() private lazy var collectionView: SPCollectionView = { let collectionView = SPCollectionView(frame: .zero, collectionViewLayout: collectionViewLayout) collectionView.delegate = self collectionView.dataSource = self collectionView.showsVerticalScrollIndicator = false SPVipAlertCell.registerCell(collectionView: collectionView) return collectionView }() private lazy var closeButton: UIButton = { let button = UIButton(type: .custom) button.setImage(UIImage(named: "close_icon_02"), for: .normal) button.addTarget(self, action: #selector(handleCloseButton), for: .touchUpInside) return button }() init(dataArr: [SPPayTemplateItem]) { super.init(frame: .zero) selectedIndex = dataArr.count - 1 self.dataArr = dataArr _setupUI() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func show(in view: UIView? = nil) -> Self { //记录弹出时间 SPVipAlertView.lastAlertDate = Date() return super.show(in: view) } @objc private func handleCloseButton() { self.dismiss() } } extension SPVipAlertView { private func _setupUI() { contentView.frame = CGRect(x: 0, y: 0, width: bgImageView.image?.size.width ?? 0, height: (bgImageView.image?.size.height ?? 0) + 66) self.addSubview(contentView) contentView.addSubview(bgImageView) contentView.addSubview(closeButton) bgImageView.addSubview(vipIconImageView) // bgImageView.addSubview(vipTitleLabel) bgImageView.addSubview(vipTipLabel1) bgImageView.addSubview(vipTipLabel2) bgImageView.addSubview(collectionView) bgImageView.snp.makeConstraints { make in make.left.right.top.equalToSuperview() make.bottom.equalToSuperview().offset(-66) } closeButton.snp.makeConstraints { make in make.centerX.equalToSuperview() make.bottom.equalToSuperview() } vipIconImageView.snp.makeConstraints { make in make.left.equalToSuperview().offset(42) // make.top.equalToSuperview().offset(57) make.top.equalToSuperview().offset(70) } // vipTitleLabel.snp.makeConstraints { make in // make.left.equalTo(vipIconImageView) // make.top.equalTo(vipIconImageView.snp.bottom).offset(11) // make.right.lessThanOrEqualToSuperview().offset(-140) // } vipTipLabel1.snp.makeConstraints { make in make.left.equalTo(vipIconImageView) make.top.equalToSuperview().offset(138) } vipTipLabel2.snp.makeConstraints { make in make.bottom.equalToSuperview().offset(-19) make.centerX.equalToSuperview() } collectionView.snp.makeConstraints { make in make.width.equalTo(collectionViewLayout.itemSize.width) make.height.equalTo(collectionViewLayout.itemSize.height * 4 + collectionViewLayout.minimumLineSpacing * 3) make.centerX.equalToSuperview() make.bottom.equalToSuperview().offset(-50) } } } //MARK: -------------- UICollectionViewDelegate & UICollectionViewDataSource -------------- extension SPVipAlertView: UICollectionViewDelegate, UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = SPVipAlertCell.dequeueReusableCell(collectionView: collectionView, indexPath: indexPath) cell.model = dataArr[indexPath.row] cell.sp_isSelected = indexPath.row == selectedIndex return cell } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.dataArr.count } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { selectedIndex = indexPath.row collectionView.reloadData() let model = dataArr[indexPath.row] SPIAPManager.manager.startRecharge(model: model, shortPlayId: nil, videoId: nil) { [weak self] finish in if finish { self?.buyFinishHandle?() self?.dismiss() } } } }