新增默写功能,播放按钮移到导航栏,输入框改为下边框样式
This commit is contained in:
@@ -17,6 +17,7 @@ final class QuizViewController: UIViewController {
|
||||
private let speakerButton = UIButton(type: .system)
|
||||
private let optionsStack = UIStackView()
|
||||
private let spellingView = SpellingInputView()
|
||||
private let dictationView = DictationInputView()
|
||||
private let spellingHintLabel = UILabel()
|
||||
private let resultLabel = UILabel()
|
||||
private let resultPhoneticLabel = UILabel()
|
||||
@@ -48,6 +49,8 @@ final class QuizViewController: UIViewController {
|
||||
super.viewDidAppear(animated)
|
||||
if config.mode == .spelling, selectedOption == nil {
|
||||
spellingView.activate()
|
||||
} else if config.mode == .dictation, selectedOption == nil {
|
||||
dictationView.activate()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,6 +94,13 @@ final class QuizViewController: UIViewController {
|
||||
nextButton.configuration = nextConfig
|
||||
nextButton.addAction(UIAction { [weak self] _ in self?.primaryButtonTapped() }, for: .touchUpInside)
|
||||
|
||||
navigationItem.rightBarButtonItem = UIBarButtonItem(
|
||||
image: UIImage(systemName: "speaker.wave.2.fill"),
|
||||
style: .plain,
|
||||
target: self,
|
||||
action: #selector(speakCurrentWord)
|
||||
)
|
||||
|
||||
let headerStack = UIStackView(arrangedSubviews: [progressView, countLabel])
|
||||
headerStack.axis = .vertical
|
||||
headerStack.spacing = 8
|
||||
@@ -100,7 +110,7 @@ final class QuizViewController: UIViewController {
|
||||
promptStack.spacing = 12
|
||||
promptStack.alignment = .center
|
||||
|
||||
let inputStack = UIStackView(arrangedSubviews: [optionsStack, spellingView, spellingHintLabel])
|
||||
let inputStack = UIStackView(arrangedSubviews: [optionsStack, spellingView, dictationView, spellingHintLabel])
|
||||
inputStack.axis = .vertical
|
||||
inputStack.spacing = 16
|
||||
|
||||
@@ -138,8 +148,11 @@ final class QuizViewController: UIViewController {
|
||||
resultPhoneticLabel.isHidden = true
|
||||
|
||||
let isSpelling = config.mode == .spelling
|
||||
optionsStack.isHidden = isSpelling
|
||||
let isDictation = config.mode == .dictation
|
||||
let isChoice = !isSpelling && !isDictation
|
||||
optionsStack.isHidden = !isChoice
|
||||
spellingView.isHidden = !isSpelling
|
||||
dictationView.isHidden = !isDictation
|
||||
spellingHintLabel.isHidden = !isSpelling
|
||||
|
||||
if isSpelling {
|
||||
@@ -153,6 +166,20 @@ final class QuizViewController: UIViewController {
|
||||
nextButton.isEnabled = false
|
||||
nextButton.isHidden = false
|
||||
spellingView.activate()
|
||||
} else if isDictation {
|
||||
dictationView.reset()
|
||||
dictationView.onChange = { [weak self] in
|
||||
guard let self else { return }
|
||||
nextButton.isEnabled = !dictationView.isEmpty
|
||||
}
|
||||
dictationView.onSubmit = { [weak self] in
|
||||
guard let self else { return }
|
||||
primaryButtonTapped()
|
||||
}
|
||||
nextButton.configuration?.title = "确认"
|
||||
nextButton.isEnabled = false
|
||||
nextButton.isHidden = false
|
||||
dictationView.activate()
|
||||
} else {
|
||||
nextButton.isHidden = true
|
||||
buildOptionButtons(for: question)
|
||||
@@ -213,8 +240,18 @@ final class QuizViewController: UIViewController {
|
||||
showResult(typed == question.answer, for: question)
|
||||
}
|
||||
|
||||
private func submitDictation(_ typed: String, for question: QuizQuestion) {
|
||||
guard selectedOption == nil else { return }
|
||||
selectedOption = typed
|
||||
if typed != question.answer {
|
||||
wrongWords.append(question.word)
|
||||
}
|
||||
dictationView.deactivate()
|
||||
showResult(typed == question.answer, for: question)
|
||||
}
|
||||
|
||||
private func showResult(_ correct: Bool, for question: QuizQuestion) {
|
||||
if config.mode == .spelling {
|
||||
if config.mode == .spelling || config.mode == .dictation {
|
||||
resultLabel.text = correct ? "正确" : "正确答案:\(question.answer)"
|
||||
resultPhoneticLabel.text = question.word.phonetic
|
||||
resultPhoneticLabel.isHidden = false
|
||||
@@ -229,10 +266,18 @@ final class QuizViewController: UIViewController {
|
||||
nextButton.isHidden = false
|
||||
}
|
||||
|
||||
@objc private func speakCurrentWord() {
|
||||
guard let question = current else { return }
|
||||
speech.speak(question.answer)
|
||||
}
|
||||
|
||||
private func primaryButtonTapped() {
|
||||
if config.mode == .spelling, selectedOption == nil {
|
||||
guard let question = current, spellingView.isComplete else { return }
|
||||
submitSpelling(spellingView.typedWord(), for: question)
|
||||
} else if config.mode == .dictation, selectedOption == nil {
|
||||
guard let question = current, !dictationView.isEmpty else { return }
|
||||
submitDictation(dictationView.text, for: question)
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
@@ -273,6 +318,8 @@ final class QuizViewController: UIViewController {
|
||||
showQuestion()
|
||||
if config.mode == .spelling {
|
||||
spellingView.activate()
|
||||
} else if config.mode == .dictation {
|
||||
dictationView.activate()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user