I have a ChoiceTextView.swift-file that contains the look of the button
var body: some View {
Text(choiceText)
.font(.title)
.foregroundColor(.white)
.frame(width: screenSize.width - 100, height: 100)
.background(
RoundedRectangle(cornerRadius: 10)
.fill(.blue)
)
}
and in my ContentView.swift in my QuizApp shows a button for every answer in my array
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)
})
}
This works fine and the app works so far and the button reacts and changes the color to green, if I press the correct answer and red, when I press the wrong answer. Sadly the color change is quite difficult to see because it just appears for a split of a second.
If I disable .fill(.blue) in my ChoiceTextView.swift, the color change is much better visible but the button still appears in blue. When I press the button the color changes for some milliseconds and the color looks darken as if both colors would be shown at the same time. So I'd like to change the button Color to either green or red and the button should show the color for a short duration of 1s.
The duration is not implemented by me yet since I already have trouble with the color being shown as I like to.
Fixed it myself while writing a reproducable code. I had too many
.fill(.blue)in my code and while cleaning it up, it startet to work.