I've noticed that if a Binding and a State are changed in the same withAnimation block, the State will be updated first, trigger a redraw, and then the Binding will be updated, triggering a second redraw. When instead of a Binding, I use two State variables, then the redraw happens just once and both values are updated at the same time:
When hitting the button, the console prints:
If I switch and use two State variables, I only see one print:
Why is this happening? Is there a way to keep using a Binding and a State but trigger a redraw just once?
Code Block struct ContentView: View { @Binding var x1: Int // @State var x1: Int @State var x2: Int = 0 var body: some View { VStack { Button(action: { withAnimation { x1 += 1 x2 += 2 } }, label: { Text("Increment") .padding() }) Text("Total: \(total)") } } var total: Int { print("\(x1) + \(x2) = \(x1 + x2)") return x1 + x2 } }
When hitting the button, the console prints:
Code Block 0 + 2 = 2 1 + 2 = 3
If I switch and use two State variables, I only see one print:
Code Block 1 + 2 = 3
Why is this happening? Is there a way to keep using a Binding and a State but trigger a redraw just once?