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

113 lines
4.2 KiB
Swift

//
// HomeLocationReportCardView.swift
// suixinkan
//
import SnapKit
import UIKit
///
final class HomeLocationReportCardView: UIView {
var onReportTap: (() -> Void)?
private let titleStackView = UIStackView()
private let titleIconView = UIImageView(image: UIImage(systemName: "arrow.up.square.fill"))
private let titleLabel = UILabel()
private let addressLabel = UILabel()
private let reportButtonContainer = UIView()
private let reportButton = UIButton(type: .system)
private let reportGradientLayer = CAGradientLayer()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = AppColor.cardBackground
layer.cornerRadius = AppRadius.md
clipsToBounds = true
titleStackView.axis = .horizontal
titleStackView.alignment = .center
titleStackView.spacing = AppSpacing.xs
titleIconView.tintColor = AppColor.primary
titleIconView.contentMode = .scaleAspectFit
titleLabel.text = "立即上报"
titleLabel.font = .systemFont(ofSize: 22, weight: .bold)
titleLabel.textColor = AppColor.textPrimary
addressLabel.font = .systemFont(ofSize: 15, weight: .regular)
addressLabel.textColor = AppColor.textTertiary
addressLabel.numberOfLines = 2
reportGradientLayer.colors = [AppColor.primary.cgColor, UIColor(hex: 0x5CA8FF).cgColor]
reportGradientLayer.startPoint = CGPoint(x: 0.5, y: 0)
reportGradientLayer.endPoint = CGPoint(x: 0.5, y: 1)
reportButtonContainer.layer.insertSublayer(reportGradientLayer, at: 0)
reportButtonContainer.layer.cornerRadius = 46
reportButtonContainer.clipsToBounds = true
reportButton.backgroundColor = .clear
reportButton.tintColor = .white
reportButton.setImage(UIImage(systemName: "hand.tap.fill"), for: .normal)
reportButton.setPreferredSymbolConfiguration(
UIImage.SymbolConfiguration(pointSize: 38, weight: .semibold),
forImageIn: .normal
)
reportButton.imageView?.contentMode = .scaleAspectFit
reportButton.addTarget(self, action: #selector(reportTapped), for: .touchUpInside)
reportButton.accessibilityLabel = "上报位置"
titleStackView.addArrangedSubview(titleIconView)
titleStackView.addArrangedSubview(titleLabel)
addSubview(titleStackView)
addSubview(addressLabel)
addSubview(reportButtonContainer)
reportButtonContainer.addSubview(reportButton)
titleIconView.snp.makeConstraints { make in
make.width.height.equalTo(25)
}
titleStackView.snp.makeConstraints { make in
make.leading.equalToSuperview().offset(15)
make.top.equalToSuperview().offset(15)
make.trailing.lessThanOrEqualTo(reportButtonContainer.snp.leading).offset(-AppSpacing.sm)
}
addressLabel.snp.makeConstraints { make in
make.top.equalTo(titleStackView.snp.bottom).offset(AppSpacing.xxs)
make.leading.equalTo(titleStackView)
make.trailing.lessThanOrEqualTo(reportButtonContainer.snp.leading).offset(-AppSpacing.sm)
}
reportButtonContainer.snp.makeConstraints { make in
make.trailing.equalToSuperview().offset(-15)
make.centerY.equalToSuperview()
make.width.height.equalTo(92)
}
reportButton.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
reportGradientLayer.frame = reportButtonContainer.bounds
reportGradientLayer.cornerRadius = reportButtonContainer.bounds.width / 2
}
func apply(address: String, isReporting: Bool) {
addressLabel.text = address
reportButton.isEnabled = !isReporting
reportButton.alpha = 1
reportGradientLayer.colors = isReporting
? [UIColor(hex: 0xC9CED6).cgColor, UIColor(hex: 0xD9DDE4).cgColor]
: [AppColor.primary.cgColor, UIColor(hex: 0x5CA8FF).cgColor]
}
@objc private func reportTapped() { onReportTap?() }
}