import SnapKit import UIKit /// 学习与考试共用的进度提示,两项等宽居中,仅突出当前步骤。 @MainActor final class TrainingStepView: UIView { /// 当前页面对应的学习流程阶段。 enum Step { case learning case exam } private let learningLabel: UILabel private let nextLabel: UILabel /// 创建只读步骤栏,任务模式可在课程详情返回后更新。 init(currentStep: Step, mode: TrainingMode = .exam) { learningLabel = TrainingUI.text("1 课程学习", size: 13, weight: currentStep == .learning ? .semibold : .regular, color: currentStep == .learning ? TrainingUI.primary : TrainingUI.muted) nextLabel = TrainingUI.text(size: 13, weight: currentStep == .exam ? .semibold : .regular, color: currentStep == .exam ? TrainingUI.primary : TrainingUI.muted) super.init(frame: .zero) backgroundColor = UIColor(red: 237/255, green: 241/255, blue: 247/255, alpha: 1) layer.cornerRadius = 8 accessibilityIdentifier = "training.steps" learningLabel.accessibilityIdentifier = "training.steps.learning" nextLabel.accessibilityIdentifier = "training.steps.next" for label in [learningLabel, nextLabel] { label.textAlignment = .center } let currentLabel = currentStep == .learning ? learningLabel : nextLabel currentLabel.accessibilityTraits.insert(.selected) currentLabel.accessibilityValue = "当前步骤" let stack = UIStackView(arrangedSubviews: [learningLabel, nextLabel]) stack.axis = .horizontal stack.alignment = .center stack.distribution = .fillEqually addSubview(stack) stack.snp.makeConstraints { $0.edges.equalToSuperview().inset(UIEdgeInsets(top: 10, left: 12, bottom: 10, right: 12)) } snp.makeConstraints { $0.height.greaterThanOrEqualTo(44) } configure(mode: mode) } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } /// 仅学习任务展示完成学习,考试任务展示在线考试。 func configure(mode: TrainingMode) { nextLabel.text = mode == .exam ? "2 在线考试" : "完成学习" } }