Animating Transitions between ViewBuilder Switch Cases

111 Views Asked by At

I have the following code

var body: some View {
        NavigationView {
            content
        }
}

@ViewBuilder private var content: some View {
        switch someEnum {
        case .case1:
            view1()
        case .case2:
            view2()
        case .case3:
            view3()
        case .case4:
            view4()
        }
}

What's the best way to add transitions when the enum is changed so the switch between two views seems seamless similar to navigation links? Right now the view on top abruptly pops into the next view. I am looking to replicate a similar transition to that of a navigation link, where one view slides into the other on change.

1

There are 1 best solutions below

3
Corey Smith On

I believe you can add a transition modifier to each view like the following.

view1()

   .transition(.scale(scale: 0.1, anchor: .bottom))

...

view2()

   .transition(.scale(scale: 0.1, anchor: .bottom))