SwiftUI条件视图转换不起作用

时间:2019-12-20 14:02:23

标签: swift animation swiftui

考虑以下代码:

                     Source 1        |        Source 2         |      Source 3
          |  Next Day; Ground; 2 Day | Next Day; Ground; 2 Day | Next Day; Ground; 2 Day 
+Manager 1| 
   Rep 1  |
   Rep 2  |
+Manager 2|
   Rep 1  |
   Rep 2  |

问题

在这种情况下,我看到从IntroView到LoginView的过渡工作正常,除了任何动画。基于条件的IntroView内部动画效果似乎不错,但更改整个屏幕的过渡效果似乎无效。

1 个答案:

答案 0 :(得分:0)

  1. group更改为ZStack

  2. 在某处添加动画。

                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)
                      }
                    }