I would like to animate a particular tapped button from the stack of buttons. Unfortunately withAnimation{} is applied to all the buttons created by FOREACH loop with no difference what button was actually tapped. Could you please recommend the right way to solve the issue? Here is my code:
import SwiftUI
struct ContentView: View {
@State private var buttonBackgroundColorRed = false
var body: some View {
VStack {
ForEach(1..<4) {num in
Button {
withAnimation {
buttonBackgroundColorRed.toggle()
}
} label: {
Text("Button \(num)")
}
.padding(10)
.background(buttonBackgroundColorRed ? .red : .blue)
.foregroundColor(.white)
.padding()
}
}
}
}