ThimraTV/MoviaBox/Class/Home/View/SPHomeTrendingCell.swift
2025-05-16 18:19:04 +08:00

178 lines
5.8 KiB
Swift

//
// SPHomeTrendingCell.swift
// MoviaBox
//
// Created by on 2025/4/14.
//
import UIKit
class SPHomeTrendingCell: SPCollectionViewCell {
var model: SPShortModel? {
didSet {
coverImageView.sp_setImage(url: model?.image_url)
titleLabel.text = model?.name
let watchCount = model?.watch_total ?? 0
if watchCount > 1000 {
let numStr = NSNumber(floatLiteral: CGFloat(watchCount) / 1000).toString(maximumFractionDigits: 1)
hotView.setTitle("\(numStr)K", for: .normal)
} else {
hotView.setTitle("\(watchCount)", for: .normal)
}
if let category = model?.category?.first {
tagView.setTitle(category, for: .normal)
tagView.isHidden = false
} else {
tagView.isHidden = true
}
}
}
var row: Int = 0 {
didSet {
let num = row + 1
// if num < 4 {
// numBgView.image = UIImage(named: "num_bg_icon_\(num)")
// } else {
// numBgView.image = UIImage(named: "num_bg_icon_4")
// }
switch num {
case 1:
numBgView.colors = [UIColor.colorD93845().cgColor, UIColor.colorD93845(alpha: 0.74).cgColor, UIColor.colorD93845(alpha: 0.17).cgColor]
case 2:
numBgView.colors = [UIColor.colorFF5528().cgColor, UIColor.colorFF5528(alpha: 0.74).cgColor, UIColor.colorFF5528(alpha: 0.17).cgColor]
case 3:
numBgView.colors = [UIColor.colorFF9924().cgColor, UIColor.colorFF9924(alpha: 0.74).cgColor, UIColor.colorFF9924(alpha: 0.17).cgColor]
default:
numBgView.colors = [UIColor.color000000(alpha: 0.45).cgColor, UIColor.color000000(alpha: 0.45).cgColor, UIColor.color000000(alpha: 0.45).cgColor]
}
numLabel.text = "\(num)"
}
}
//MARK: UI
private lazy var coverImageView: SPImageView = {
let imageView = SPImageView()
imageView.layer.cornerRadius = 6
imageView.layer.masksToBounds = true
return imageView
}()
private lazy var numBgView: SPGradientView = {
let view = SPGradientView()
view.locations = [0, 0.5, 1]
view.startPoint = .init(x: 0.5, y: 0)
view.endPoint = .init(x: 0.5, y: 1)
view.addRadius(topLeft: 0, topRight: 0, bottomLeft: 0, bottomRight: 4)
return view
}()
private lazy var numLabel: UILabel = {
let label = UILabel()
label.font = .fontMedium(ofSize: 20)
label.textColor = .colorFFFFFF()
return label
}()
private lazy var titleLabel: UILabel = {
let label = UILabel()
label.font = .fontMedium(ofSize: 12)
label.textColor = .colorFFFFFF()
label.numberOfLines = 2
return label
}()
private lazy var hotView: JXButton = {
let button = JXButton(type: .custom)
button.isUserInteractionEnabled = false
button.titleDirection = .right
button.jx_font = .fontRegular(ofSize: 10)
button.leftAndRightMargin = 6
button.setImage(UIImage(named: "hot_icon_01"), for: .normal)
button.setTitleColor(.colorFFDD00(), for: .normal)
button.backgroundColor = .colorD9D9D9(alpha: 0.16)
button.layer.cornerRadius = 2
button.layer.masksToBounds = true
return button
}()
private lazy var tagView: JXButton = {
let button = JXButton(type: .custom)
button.isUserInteractionEnabled = false
button.backgroundColor = .colorD9D9D9(alpha: 0.16)
button.layer.cornerRadius = 2
button.layer.masksToBounds = true
button.jx_font = .fontRegular(ofSize: 10)
button.setTitleColor(.colorFFFFFF(), for: .normal)
button.leftAndRightMargin = 6
return button
}()
override init(frame: CGRect) {
super.init(frame: frame)
_setupUI()
}
@MainActor required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension SPHomeTrendingCell {
private func _setupUI() {
contentView.addSubview(coverImageView)
coverImageView.addSubview(numBgView)
numBgView.addSubview(numLabel)
contentView.addSubview(titleLabel)
contentView.addSubview(hotView)
contentView.addSubview(tagView)
// coverImageView.addSubview(hotBgView)
// hotBgView.addSubview(hotIconImageView)
coverImageView.snp.makeConstraints { make in
make.left.top.bottom.equalToSuperview()
make.width.equalTo(82)
}
numBgView.snp.makeConstraints { make in
make.left.top.equalToSuperview()
make.width.equalTo(24)
make.height.equalTo(32)
}
numLabel.snp.makeConstraints { make in
make.center.equalToSuperview()
}
titleLabel.snp.makeConstraints { make in
make.left.equalTo(coverImageView.snp.right).offset(8)
make.top.equalToSuperview().offset(12)
make.right.lessThanOrEqualToSuperview().offset(-5)
}
hotView.snp.makeConstraints { make in
make.left.equalTo(titleLabel)
make.top.equalToSuperview().offset(56)
make.height.equalTo(18)
}
tagView.snp.makeConstraints { make in
make.left.equalTo(hotView)
make.top.equalTo(hotView.snp.bottom).offset(8)
make.height.equalTo(18)
}
}
}