I’m trying to change the background color of the Text
component, whenever a given condition is met, in this example, when numberOfTaps = 3
, which I’m able to do by having the condition inside the .background
property call
What I’m trying to do is to change the background with an animation, nothing too fancy, just something like .easeInOut
might work; how can I do that?
import SwiftUI
struct ContentView: View {
@State private var numberOfTaps = 0
var body: some View {
VStack {
Button("Up") {
numberOfTaps += 1
}
Text("(numberOfTaps)")
.padding()
.background(numberOfTaps == 3 ? Color.blue : Color.green)
Button("Down") {
numberOfTaps -= 1
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Here’s a sample of the current UI:
2
Answers
I have modified your body. Lets try this for easeInOut animation:-
Wrap your conditions inside the withAnimation{} will automatically apply animation to all the related Views to these conditions/data