How can I disable animation just for first render

I have fully working code and I am okay with my approach, the only issue that I am facing is having animation at app lunch, my approach force my view to animate on onAppear, which is okay, but I want my view appear with no animation when my App lunch for first time, then the code should work as coded. How can I make my view put a deference on very first onAppear and the other onAppear that happens because of Button. Looking for solving the issue in my question in simplest and easiest way possible.

 struct ContentView: View {
    
    @State private var showView: Bool = true
    
    var body: some View {
        
        if (showView) { MyView() }
        
        Button("toggle") { showView.toggle() }
        
    }
}

struct MyView: View {
    @State private var start: Bool = Bool()
    
    var body: some View {
        Circle()
            .frame(width: start ? 300.0 : 100.0, height: start ? 300.0 : 100.0)
            .onAppear(perform: { start.toggle() })
            .animation(.default, value: start)
    }
}

1 answer

  • answered 2022-01-19 16:45 Yrb

    Instead of using:

    .animation(.default, value: start)
    

    wrap wherever you change start and want it animated with:

    withAnimation(.default) {
        start.toggle()
    }
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum