I have a problem with animating buttons. I have them created using ForEach method. The layout of my app consists of 3 flags. We have text that is saying that we have to choose a flag of particular country and if we choose the correct one we gain points.
I want to animate particular button when it is pressed. Doesn't matter if the answer is correct or not.
My code for creating these buttons looks like this:
ForEach(0 ..< 3) { number in
Button(action: {
withAnimation {
if number == self.correctAnswer {
self.animationAmount += 360
}
}
self.flagTapped(number)
}) {
ImageView(myImage: self.countries[number])
}
.rotation3DEffect(.degrees(number == self.correctAnswer ? self.animationAmount : 0), axis: (x: 0, y: 1, z: 0))
.opacity(self.showAnimation && number != self.correctAnswer ? 0.25 : 1)
.background(self.showAnimation && number != self.correctAnswer ? Color.red: nil)
}
self.flagTapped(number) toggles showAnimation to be true.
