Files
suixinkan_uikit/suixinkan/UI/Home/Views/AllFunctionMenuCell.swift

117 lines
3.7 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// AllFunctionMenuCell.swift
// suixinkan
//
import SnapKit
import UIKit
///
final class AllFunctionMenuCell: UICollectionViewCell {
static let reuseIdentifier = "AllFunctionMenuCell"
///
enum ActionStyle {
case none
case add
case remove
}
private let cardView = UIView()
private let contentStackView = UIStackView()
private let iconView = UIImageView()
private let titleLabel = UILabel()
private let actionImageView = UIImageView()
override var isHighlighted: Bool {
didSet {
cardView.alpha = isHighlighted ? 0.72 : 1
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
///
func apply(menu: HomeMenuItem, actionStyle: ActionStyle) {
iconView.image = HomeMenuIconFactory.image(named: menu.iconName)
titleLabel.text = menu.title
accessibilityLabel = menu.title
switch actionStyle {
case .none:
actionImageView.isHidden = true
accessibilityTraits = []
accessibilityHint = nil
case .add:
actionImageView.isHidden = false
actionImageView.image = UIImage(systemName: "plus.circle.fill")
actionImageView.tintColor = AppColor.primary
accessibilityTraits = .button
accessibilityHint = "添加到常用应用"
case .remove:
actionImageView.isHidden = false
actionImageView.image = UIImage(systemName: "minus.circle.fill")
actionImageView.tintColor = AppColor.danger
accessibilityTraits = .button
accessibilityHint = "移出常用应用"
}
}
private func setupUI() {
contentView.backgroundColor = .clear
cardView.backgroundColor = .white
cardView.layer.cornerRadius = AppRadius.md
cardView.clipsToBounds = true
iconView.tintColor = AppColor.primary
iconView.contentMode = .center
iconView.adjustsImageSizeForAccessibilityContentSizeCategory = false
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
isAccessibilityElement = true
actionImageView.contentMode = .scaleAspectFit
actionImageView.isUserInteractionEnabled = false
contentView.addSubview(cardView)
cardView.addSubview(contentStackView)
cardView.addSubview(actionImageView)
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)
}
actionImageView.snp.makeConstraints { make in
make.top.trailing.equalToSuperview().inset(AppSpacing.xxs)
make.width.height.equalTo(20)
}
}
}