重构首页为单一集合视图并添加全部功能自定义

This commit is contained in:
2026-07-07 09:57:41 +08:00
parent 16e86c8899
commit af450fb4cc
14 changed files with 1222 additions and 209 deletions

View File

@ -0,0 +1,104 @@
//
// AllFunctionMenuCell.swift
// suixinkan
//
import SnapKit
import UIKit
///
final class AllFunctionMenuCell: UICollectionViewCell {
static let reuseIdentifier = "AllFunctionMenuCell"
enum ActionStyle {
case none
case add
case remove
}
var onActionTap: (() -> Void)?
private let cardView = UIView()
private let iconView = UIImageView()
private let titleLabel = UILabel()
private let actionButton = UIButton(type: .system)
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepareForReuse() {
super.prepareForReuse()
onActionTap = nil
}
func apply(menu: HomeMenuItem, actionStyle: ActionStyle) {
iconView.image = UIImage(systemName: menu.iconName)
titleLabel.text = menu.title
switch actionStyle {
case .none:
actionButton.isHidden = true
case .add:
actionButton.isHidden = false
actionButton.setImage(UIImage(systemName: "plus.circle.fill"), for: .normal)
actionButton.tintColor = AppColor.primary
case .remove:
actionButton.isHidden = false
actionButton.setImage(UIImage(systemName: "minus.circle.fill"), for: .normal)
actionButton.tintColor = AppColor.danger
}
}
private func setupUI() {
contentView.backgroundColor = .clear
cardView.backgroundColor = .white
cardView.layer.cornerRadius = AppRadius.md
cardView.clipsToBounds = true
iconView.tintColor = AppColor.primary
iconView.contentMode = .scaleAspectFit
titleLabel.font = .app(.caption)
titleLabel.textColor = AppColor.textPrimary
titleLabel.textAlignment = .center
titleLabel.numberOfLines = 2
actionButton.addTarget(self, action: #selector(actionTapped), for: .touchUpInside)
contentView.addSubview(cardView)
cardView.addSubview(iconView)
cardView.addSubview(titleLabel)
cardView.addSubview(actionButton)
cardView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
iconView.snp.makeConstraints { make in
make.top.equalToSuperview().offset(AppSpacing.sm)
make.centerX.equalToSuperview()
make.width.height.equalTo(32)
}
titleLabel.snp.makeConstraints { make in
make.top.equalTo(iconView.snp.bottom).offset(AppSpacing.xs)
make.leading.trailing.equalToSuperview().inset(AppSpacing.xxs)
make.bottom.lessThanOrEqualToSuperview().inset(AppSpacing.xs)
}
actionButton.snp.makeConstraints { make in
make.top.trailing.equalToSuperview().inset(AppSpacing.xxs)
make.width.height.equalTo(20)
}
}
@objc private func actionTapped() {
onActionTap?()
}
}