叠加过渡问题

Transition issue on overlay

我有一个叠加层,它会在切换为真时显示。当它显示时,它应该有一个从视图的右到左的过渡,以便在视图之间实现良好的过渡。问题是动画从右上角以对角线运动向下移动。

我假设动画不正确或者叠加层的动画有问题。

预先感谢您的帮助。

struct createUserAccount: View {
    let transition: AnyTransition = .asymmetric(insertion: .move(edge:.trailing), removal: .move(edge: .leading))
    //Stores form info as userInfo, stored in userModel
    @ObservedObject var userInformation = FormViewModel()
    @ObservedObject var keyboardResponder = KeyboardResponder()
    @State private var showFitnessForm = false

    @Environment (\.managedObjectContext) var moc //calls managed Object context (datacore)
    @Environment (\.dismiss) var dismiss
    @FetchRequest(sortDescriptors: []) var allUsers: FetchedResults <User>
    
    init(){
        UITableView.appearance().backgroundColor = .clear
    }
  
    var body: some View {
            ZStack{
                Form{
                    Section(){
                        TextField("FirstName", text: $userInformation.firstname)
                        TextField("LastName", text: $userInformation.lastname)
                        TextField("Email", text: $userInformation.email)
                            if userInformation.emailPrompt != "" {
                                Text(userInformation.emailPrompt)
                                    .font(.caption).italic().foregroundColor(.red)
                                
                            }
                        SecureField("Passsword", text: $userInformation.password
                                    ).autocapitalization(.none)
                            if userInformation.passwordPrompt != "" {
                                Text(userInformation.passwordPrompt)
                                    .font(.caption).italic().foregroundColor(.red)
                            }
                        }
                            .listRowBackground(Color.clear)
                            .padding()
                            .navigationBarTitle(Text("Lets Get Started"))
                            .navigationBarTitleDisplayMode(.automatic)
                            .font(.system(size:18))
                            .listStyle(GroupedListStyle())
                           
                        }
                                Button("Continue"){
                                    let newUser = User(context: moc)
                                    newUser.id = UUID()
                                    newUser.firstName = userInformation.firstname
                                    newUser.lastName = userInformation.lastname
                                    newUser.email = userInformation.email
                                    newUser.password = userInformation.password
                                    showFitnessForm.toggle()
                                   // dismiss()
                                }
                                    .frame(width: 150, height: 20)
                                    .foregroundColor(.white)
                                    .padding()
                                    .background(LinearGradient(gradient: Gradient(colors: [.orange, .pink]), startPoint: .leading, endPoint: .bottom))
                                    .font(.title3)
                                    .background(.clear)
                                    .cornerRadius(5)
                                    .offset(y:30)
                                    .opacity(userInformation.isSignUpComplete ? 1 : 0.5)
                                    .offset(y: keyboardResponder.currentHeight*2)
                                    .disabled(!userInformation.isSignUpComplete)
                                    .frame(height: 300)
        
                            HStack{
                                Text("Already a User?").italic().font(.callout)
                                NavigationLink(destination: userLogin() .navigationBarHidden(true)){
                                    Text("Login")
                                        .foregroundColor(.pink).font(.callout)
                                }
                            }
                .offset(y:80)
                .offset(y: keyboardResponder.currentHeight*2)
               
            }
        
            .overlay(
                showFitnessForm ? FitnessForm(
                userFirstName: $userInformation.firstname,
                userLastName: $userInformation.lastname,
                userEmailAddress: $userInformation.email,
                userLoginPassword: $userInformation.password
                ) .animation(Animation.easeInOut(duration: 1.0), value: offset)
                    .transition(transition)  : nil )
    }
}

试试这个

.overlay(
   VStack {
      if showFitnessForm {
        FitnessForm(...)
          .transition(transition)   // << here !!
      }
   }
   .animation(Animation.easeInOut(duration: 1.0), value: showFitnessForm)  // << here !!
)

*about diagoanal movement - 我假设你的视图在 NavigationView 中,所以看看这个 Broken animation in NavigationView