148 lines
4.4 KiB
Swift
148 lines
4.4 KiB
Swift
//
|
|
// XSSearchHotListItemView.swift
|
|
// XSeri
|
|
//
|
|
// Created by 长沙鸿瑶 on 2026/2/2.
|
|
//
|
|
|
|
import UIKit
|
|
import SnapKit
|
|
|
|
final class XSSearchHotListItemView: UIView {
|
|
|
|
/// 点击条目回调
|
|
var didTap: ((_ model: XSShortModel) -> Void)?
|
|
|
|
private var model: XSShortModel?
|
|
|
|
private lazy var rankBadgeView: XSView = {
|
|
let view = XSView()
|
|
view.xs_startPoint = .init(x: 0.5, y: 0)
|
|
view.xs_endPoint = .init(x: 0.5, y: 1)
|
|
view.layer.masksToBounds = true
|
|
return view
|
|
}()
|
|
|
|
private lazy var rankLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.font = .font(ofSize: 10, weight: .bold)
|
|
return label
|
|
}()
|
|
|
|
private lazy var coverImageView: XSImageView = {
|
|
let imageView = XSImageView()
|
|
imageView.layer.cornerRadius = 4
|
|
return imageView
|
|
}()
|
|
|
|
private lazy var titleLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.font = .font(ofSize: 10, weight: .semibold)
|
|
label.textColor = UIColor.white.withAlphaComponent(0.9)
|
|
label.numberOfLines = 2
|
|
return label
|
|
}()
|
|
|
|
private lazy var categoryLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.font = .font(ofSize: 10, weight: .medium)
|
|
label.textColor = UIColor.white.withAlphaComponent(0.6)
|
|
return label
|
|
}()
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
xs_setupUI()
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
override func layoutSubviews() {
|
|
super.layoutSubviews()
|
|
rankBadgeView.layer.cornerRadius = rankBadgeView.bounds.height / 2
|
|
}
|
|
|
|
func configure(model: XSShortModel, rank: Int, style: XSSearchHotSectionStyle) {
|
|
// 根据榜单样式调整数字徽标的颜色
|
|
self.model = model
|
|
rankLabel.text = "\(rank)"
|
|
titleLabel.text = model.name ?? model.xs_description
|
|
let category = model.category?.first ?? model.categoryList?.first?.name ?? ""
|
|
categoryLabel.text = category
|
|
categoryLabel.isHidden = category.isEmpty
|
|
coverImageView.xs_setImage(model.image_url)
|
|
|
|
switch style {
|
|
case .gold:
|
|
if rank <= 3 {
|
|
rankBadgeView.xs_colors = [UIColor.FFE_6_B_3.cgColor, UIColor.F_4_C_783.cgColor]
|
|
rankLabel.textColor = ._060606
|
|
} else {
|
|
rankBadgeView.xs_colors = [UIColor.clear.cgColor, UIColor.clear.cgColor]
|
|
rankLabel.textColor = .FCD_68_D
|
|
}
|
|
case .purple:
|
|
if rank <= 3 {
|
|
rankBadgeView.xs_colors = [UIColor.E_7_CCFF.cgColor, UIColor.D_7_BCFF.cgColor, UIColor.A_191_FF.cgColor]
|
|
rankLabel.textColor = .black
|
|
} else {
|
|
rankBadgeView.xs_colors = [UIColor.clear.cgColor, UIColor.clear.cgColor]
|
|
rankLabel.textColor = UIColor.B_4_A_0_FF
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
extension XSSearchHotListItemView {
|
|
|
|
private func xs_setupUI() {
|
|
let tap = UITapGestureRecognizer(target: self, action: #selector(xs_handleTap))
|
|
addGestureRecognizer(tap)
|
|
isUserInteractionEnabled = true
|
|
|
|
addSubview(rankBadgeView)
|
|
rankBadgeView.addSubview(rankLabel)
|
|
addSubview(coverImageView)
|
|
addSubview(titleLabel)
|
|
addSubview(categoryLabel)
|
|
|
|
rankBadgeView.snp.makeConstraints { make in
|
|
make.left.equalToSuperview()
|
|
make.centerY.equalToSuperview()
|
|
make.size.equalTo(CGSize(width: 14, height: 14))
|
|
}
|
|
|
|
rankLabel.snp.makeConstraints { make in
|
|
make.center.equalToSuperview()
|
|
}
|
|
|
|
coverImageView.snp.makeConstraints { make in
|
|
make.left.equalTo(rankBadgeView.snp.right).offset(8)
|
|
make.centerY.equalToSuperview()
|
|
make.size.equalTo(CGSize(width: 48, height: 65))
|
|
}
|
|
|
|
titleLabel.snp.makeConstraints { make in
|
|
make.left.equalTo(coverImageView.snp.right).offset(10)
|
|
make.top.equalToSuperview()
|
|
make.right.lessThanOrEqualToSuperview()
|
|
}
|
|
|
|
categoryLabel.snp.makeConstraints { make in
|
|
make.left.equalTo(titleLabel)
|
|
make.top.equalTo(titleLabel.snp.bottom).offset(6)
|
|
make.right.lessThanOrEqualToSuperview()
|
|
}
|
|
}
|
|
}
|
|
|
|
extension XSSearchHotListItemView {
|
|
|
|
@objc private func xs_handleTap() {
|
|
guard let model = model else { return }
|
|
didTap?(model)
|
|
}
|
|
}
|