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!

Alexnnd
  • 429
  • 4
  • 13
  • Does [this post](https://stackoverflow.com/q/56907344/5133585) answer your question? – Sweeper Jun 08 '23 at 12:38
  • It seems to work but not really and this also creates a funny bug with my `ToolbarItem`. – Alexnnd Jun 08 '23 at 13:00
  • You didn't say anything about `ToolbarItem` in your question. Please [edit] it to show a [mcve], of how the linked question does not answer your question. – Sweeper Jun 08 '23 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! – Alexnnd Jun 08 '23 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! – Alexnnd Jun 08 '23 at 13:22
  • PLEASE: I don't know who made my question a duplicate because the proposed solution in the link doesn't work for me. THE SOLUTION is just above! – Alexnnd Jun 08 '23 at 13:26

0 Answers0