113 lines
3.3 KiB
Swift
113 lines
3.3 KiB
Swift
//
|
|
// VPHomeRecommandCell.swift
|
|
// Veloria
|
|
//
|
|
// Created by Veloria on 2025/5/20.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class VPHomeRecommandCell: VPCollectionViewCell {
|
|
|
|
enum CellPoint {
|
|
case start
|
|
case end
|
|
case other
|
|
}
|
|
|
|
var model: VPShortModel? {
|
|
didSet {
|
|
coverImageView.vp_setImage(url: model?.image_url)
|
|
titleLabel.text = model?.name
|
|
}
|
|
}
|
|
|
|
var cellPoint = CellPoint.other {
|
|
didSet {
|
|
applyDiagonalMask()
|
|
}
|
|
}
|
|
|
|
private lazy var coverImageView: VPImageView = {
|
|
let imageView = VPImageView()
|
|
return imageView
|
|
}()
|
|
|
|
private lazy var titleLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.font = .fontRegular(ofSize: 12)
|
|
label.textColor = .colorFFFFFF()
|
|
label.numberOfLines = 2
|
|
return label
|
|
}()
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
vp_setupUI()
|
|
|
|
}
|
|
|
|
@MainActor required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
|
|
private func applyDiagonalMask() {
|
|
let maskLayer = CAShapeLayer()
|
|
let path = UIBezierPath()
|
|
let width = 137.0
|
|
let height = 152.0
|
|
let cut: CGFloat = 19.5 // 控制斜切角度
|
|
let radius: CGFloat = 14 // 圆角半径
|
|
|
|
if cellPoint == .start {
|
|
path.move(to: CGPoint(x: 0, y: radius))
|
|
path.addQuadCurve(to: CGPoint(x: radius, y: 0), controlPoint: CGPoint(x: 0, y: 0))
|
|
} else {
|
|
path.move(to: CGPoint(x: cut - 2, y: radius))
|
|
path.addQuadCurve(to: CGPoint(x: cut + radius, y: 0), controlPoint: CGPoint(x: cut, y: 0))
|
|
}
|
|
|
|
// 右上角
|
|
path.addLine(to: CGPoint(x: width - radius, y: 0))
|
|
path.addQuadCurve(to: CGPoint(x: width, y: radius), controlPoint: CGPoint(x: width, y: 0))
|
|
// 右下角
|
|
if cellPoint == .end {
|
|
path.addLine(to: CGPoint(x: width, y: height - radius))
|
|
path.addQuadCurve(to: CGPoint(x: width - radius, y: height), controlPoint: CGPoint(x: width, y: height))
|
|
} else {
|
|
path.addLine(to: CGPoint(x: width - cut, y: height - radius))
|
|
path.addQuadCurve(to: CGPoint(x: width - cut - radius, y: height), controlPoint: CGPoint(x: width - cut, y: height))
|
|
}
|
|
// 左下角
|
|
path.addLine(to: CGPoint(x: radius, y: height))
|
|
path.addQuadCurve(to: CGPoint(x: 0, y: height - radius), controlPoint: CGPoint(x: 0, y: height))
|
|
path.close()
|
|
|
|
maskLayer.path = path.cgPath
|
|
coverImageView.layer.mask = maskLayer
|
|
}
|
|
}
|
|
|
|
extension VPHomeRecommandCell {
|
|
|
|
private func vp_setupUI() {
|
|
contentView.addSubview(coverImageView)
|
|
contentView.addSubview(titleLabel)
|
|
|
|
coverImageView.snp.makeConstraints { make in
|
|
make.left.equalToSuperview()
|
|
make.right.equalToSuperview()
|
|
make.top.equalToSuperview()
|
|
make.height.equalTo(152)
|
|
}
|
|
|
|
titleLabel.snp.makeConstraints { make in
|
|
make.left.equalToSuperview()
|
|
make.top.equalTo(coverImageView.snp.bottom).offset(4)
|
|
make.right.lessThanOrEqualToSuperview().offset(-15)
|
|
}
|
|
}
|
|
|
|
}
|