p0lAris
p0lAris

Reputation: 4820

SwiftUI Conditional View Transitions are not working

Consider the following code:

class ApplicationHostingView: ObservableObject {
  @Published var value: Bool
}

struct ApplicationHostingView: View {
  // view model env obj
  var body: some View {
    Group {
      if applicationHostingViewModel.value {
        LoginView()
          .transition(.move(edge: .leading)) // <<<< Transition for Login View
      } else {
        IntroView()
      }
    }
  }
}

struct IntroView: View {
  // view model env obj
  var body: some View {
    Button(action: { applicationHostingViewModel.value = true }) {
      Text("Continue")
    }
  }
}

struct LoginView: View {
  var body: some View {
    Text("Hello World")
  }
}

ISSUE

In this case, I see my transition from IntroView to LoginView work fine except for any of the animations. Animations inside IntroView based on the conditionals seem to be working fine but transitions that change the entire screen don't seem to work.

Upvotes: 1

Views: 931

Answers (1)

E.Coms
E.Coms

Reputation: 11529

  1. change group to ZStack

  2. add animation somewhere.

                class ApplicationHostingViewModel: ObservableObject {
                      @Published var value: Bool = false
                    }
    
                    struct ApplicationHostingView: View {
                      // view model env obj
                        @ObservedObject var applicationHostingViewModel : ApplicationHostingViewModel
                      var body: some View {
                        ZStack {
    
                          if applicationHostingViewModel.value {
                            LoginView()
                              .transition(.move(edge: .leading))
                          } else {
                            IntroView(applicationHostingViewModel:applicationHostingViewModel)
                          }
                        }
                      }
                    }
    
                    struct IntroView: View {
                      // view model env obj
                         @ObservedObject var applicationHostingViewModel : ApplicationHostingViewModel
                      var body: some View {
                        Button(action: {
                            withAnimation(.default){
                                self.applicationHostingViewModel.value = true} }) {
                          Text("Continue")
                        }
                      }
                    }
    
                    struct LoginView: View {
                      var body: some View {
                        Text("Hello World").frame(maxWidth: .infinity, maxHeight:  .infinity)
                      }
                    }
    

Upvotes: 1

Related Questions