0

In SwiftUI, I've got an overarching view setup like this:

import SwiftUI


struct ContentView: View {
    @State var index: Int = 0
    var body: some View {
        if self.index == 0{
            FirstView(index: $index)
        }
        if self.index == 1 {
            SecondView(index: $index)
                .transition(.move(edge: .bottom))
                .animation(.easeIn)
        }

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Where I have a button in FirstView that is equivalent of:

Button(action: {
   self.index = 1
}){
   Text("Change view")
}

This works fine and all is good, but I notice that, with this method, any views conditional on an if/else statement in the child views with a transition animation (i.e. SecondView in this case), also have these animations applied.

So, for example, if SecondView was as follows:

struct SecondView: View {
    @Binding var index: Int
    @State var boolean: Bool = false
    var body: some View {
        VStack{
            if self.boolean == true {
                Text("Hello!")
            }
            Button(action: {
                self.boolean = true
            }){
                Text("change boolean")
            }
        }
    }
}

the Text("Hello") would also have the .transition(.move(edge: .bottom)) transition.

Is there any way to prevent this/a better way to create a transition animation from one view to another?

2
  • Does this answer your question stackoverflow.com/a/60785822/12299030? Commented May 30, 2021 at 5:16
  • Yeah I've seen the one liner .transition(AnyTransition.scale.animation(.easeInOut(duration: 1))) but that doesn't seem to have quite the same effect as the two liner I have in my code, since it seems to only animate certain elements of the view, as opposed to having the entire view slide in -- perhaps I'll make a new post at some point and reference the linked post and the specific example of what I mean. Commented May 30, 2021 at 15:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.