0

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!

6
  • Does this post answer your question? Commented Jun 8, 2023 at 12:38
  • It seems to work but not really and this also creates a funny bug with my ToolbarItem. Commented Jun 8, 2023 at 13:00
  • You didn't say anything about ToolbarItem in your question. Please edit it to show a minimal reproducible example, of how the linked question does not answer your question. Commented Jun 8, 2023 at 13:01
  • Actually I didn't showed my toolbar() because there's no reason. What I want to achieve and my toolbar aren't linked but for an obscure reason with the solution from your link, one of my ToolbarItem is "offseted" during the animation. It's quite funny! Commented Jun 8, 2023 at 13:20
  • I finally used my previous code which is .animation(.easeInOut(duration: 1), value: isAnimated) repeated 3 times as I can't reduce them with just one line of code with withAnimation(). Thanks anyway! Commented Jun 8, 2023 at 13:22

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.