My animateExample()
func works because the image and the 2 texts are updated twice but I can't make it "nice" with an animation. For a reason I don't know, withAnimation(.easeInOut)
does nothing. I just want a simple fade animation and not only a brutal change of the image and texts.
What's wrong with my current code?
struct ExampleView: View {
@State private var isAnimated = true
private let date = Date()
var body: some View {
HStack {
ZStack {
Circle()
.onAppear { animateExample() }
Image(isAnimated ? "city" : "sedan")
}
VStack {
Text(isAnimated ? "Renault Clio" : "Audi A8")
Text(isAnimated ? "City - \(date, format: .dateTime.day(.twoDigits).month(.twoDigits))" : "Sedan - \(date, format: .dateTime.day(.twoDigits).month(.twoDigits))")
}
}
}
func animateExample() {
withAnimation(.easeInOut) {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
isAnimated = false
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
isAnimated = true
}
}
}
}
}
Bonus question: nothing to see with what I want to achieve but is there a possibility to not repeat the date
in the second text?
Thanks in advance!