109 lines
3.2 KiB
Swift
109 lines
3.2 KiB
Swift
//
|
||
// 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 contentStackView = UIStackView()
|
||
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
|
||
|
||
contentStackView.axis = .vertical
|
||
contentStackView.alignment = .center
|
||
contentStackView.spacing = AppSpacing.sm
|
||
contentStackView.isUserInteractionEnabled = false
|
||
|
||
titleLabel.font = .app(.body)
|
||
titleLabel.textColor = AppColor.textSecondary
|
||
titleLabel.textAlignment = .center
|
||
titleLabel.numberOfLines = 2
|
||
|
||
actionButton.addTarget(self, action: #selector(actionTapped), for: .touchUpInside)
|
||
|
||
contentView.addSubview(cardView)
|
||
cardView.addSubview(contentStackView)
|
||
cardView.addSubview(actionButton)
|
||
contentStackView.addArrangedSubview(iconView)
|
||
contentStackView.addArrangedSubview(titleLabel)
|
||
|
||
cardView.snp.makeConstraints { make in
|
||
make.edges.equalToSuperview()
|
||
}
|
||
contentStackView.snp.makeConstraints { make in
|
||
make.center.equalToSuperview()
|
||
make.leading.trailing.equalToSuperview().inset(AppSpacing.xxs)
|
||
}
|
||
iconView.snp.makeConstraints { make in
|
||
make.width.height.equalTo(26)
|
||
}
|
||
actionButton.snp.makeConstraints { make in
|
||
make.top.trailing.equalToSuperview().inset(AppSpacing.xxs)
|
||
make.width.height.equalTo(20)
|
||
}
|
||
}
|
||
|
||
@objc private func actionTapped() {
|
||
onActionTap?()
|
||
}
|
||
}
|