我有一个 ChoiceTextView.swift 文件,其中包含按钮的外观
var body: some View {
Text(choiceText)
.font(.title)
.foregroundColor(.white)
.frame(width: screenSize.width - 100, height: 100)
.background(
RoundedRectangle(cornerRadius: 10)
.fill(.blue)
)
}
在 QuizApp 的 ContentView.swift 中,为数组中的每个答案显示一个按钮
ForEach(0..<question.possibleAnswers.count) { answerIndex in
Button(action: {
self.checkAnswer(answerIndex)
}, label: {
ChoiceTextView(choiceText: question.possibleAnswers[answerIndex])
.frame(width: screenSize.width - 100, height: 100)
.background(
RoundedRectangle(cornerRadius: 10)
.fill(answerIndex == selectedAnswerIndex && answerIndex == question.correctAnswer ? Color.green : Color.red)
)
.padding(5)
})
}
这工作正常,应用程序到目前为止工作正常,如果我按正确答案,按钮会做出反应并将颜色更改为绿色,当我按错误答案时,按钮会更改为红色。遗憾的是,颜色变化很难看到,因为它只出现一瞬间。
如果我在 ChoiceTextView.swift 中禁用
.fill(.blue)
,颜色变化会更明显,但按钮仍然显示为蓝色。当我按下按钮时,颜色会改变几毫秒,并且颜色看起来会变暗,就好像两种颜色会同时显示一样。所以我想将按钮颜色更改为绿色或红色,并且按钮应显示颜色短暂的 1 秒。
我还没有实现持续时间,因为我已经无法按照我喜欢的方式显示颜色了。
在编写可重现的代码时自己修复了它。我的代码中有太多
.fill(.blue)
,在清理它时,它开始工作。