AnswerBun.com

Animation does not work when passing function argument to child view

Stack Overflow Asked by user1354934 on January 3, 2022

I have a parent view that can show one of two views:

struct LoginSubView: View {
    @State private var loginSubView: LoginSubViews = .login
    
    func setLoginSubView(_ view: LoginSubViews) {
        withAnimation {
            self.loginSubView = view
        }
    }
    
    var body: some View {
        VStack {
            if self.loginSubView == .signup {
                SignupView(setLoginSubView: self.setLoginSubView)
                    .transition(AnyTransition.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)).animation(.default))
            } else {
                LoginView(setLoginSubView: self.setLoginSubView)
                    .transition(AnyTransition.asymmetric(insertion: .move(edge: .leading), removal: .move(edge: .trailing)).animation(.default))
            }
        }
        .padding(.horizontal, 16)
        .keyboardObserving()
    }
}

The setLoginSubView() is called by those child views to switch views.

Unfortunately, it no longer animates when I pass the function and call it from the child. It switches instantly. However, it does work if I do this:

struct LoginSubView: View {
    @State private var loginSubView: LoginSubViews = .login
    
    // removed setLoginSubview()
    
    var body: some View {
        VStack {
            if self.loginSubView == .signup {
                SignupView(setLoginSubView: self.setLoginSubView)
                    .transition(AnyTransition.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)).animation(.default))
                
                // Added it here
                Button(action: {
                    withAnimation {
                        self.loginSubView = .login
                    }
                }) {
                    Text("Already have an account? Sign in").animation(nil)
                }
            } else {
                LoginView(setLoginSubView: self.setLoginSubView)
                    .transition(AnyTransition.asymmetric(insertion: .move(edge: .leading), removal: .move(edge: .trailing)).animation(.default))
                // And added this here
                Button(action: {
                    withAnimation {
                        self.loginSubView = .signup
                    }
                }) {
                    Text("Don't have an account? Sign up").animation(nil)
                }
            }
        }
        .padding(.horizontal, 16)
        .keyboardObserving()
    }
}

How can I have the animation work when passing this function to the child?

One Answer

Move animation to container (tested with Xcode 12 / iOS 14)

var body: some View {
    VStack {
        if self.loginSubView == .signup {
            SignupView(setLoginSubView: self.setLoginSubView)
                .transition(AnyTransition.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))
        } else {
            LoginView(setLoginSubView: self.setLoginSubView)
                .transition(AnyTransition.asymmetric(insertion: .move(edge: .leading), removal: .move(edge: .trailing)))
        }
    }
    .animation(.default)          // << here !!
    .padding(.horizontal, 16)
    .keyboardObserving()
}

Answered by Asperi on January 3, 2022

Add your own answers!

Related Questions

Red box isn’t placed as expected

1  Asked on November 4, 2021 by liya-thomas

       

String Comparison Javascript

2  Asked on November 4, 2021 by crimsonpython24

       

es5 using promise but returns defered before resolving

2  Asked on November 4, 2021 by rodney-wormsbecher

     

2 arrays with json problem (Node.js, Discord.js)

3  Asked on November 4, 2021 by mr-code

     

Is there a XAML equivalent to nameof?

2  Asked on November 4, 2021 by luxun

       

Multistage builds with yum

3  Asked on November 4, 2021 by vorticity

 

WooCommerce can’t see product variations

1  Asked on November 4, 2021 by tizio-fittizio

       

VScode setup with ROS and auto complete

3  Asked on November 4, 2021 by dixcipuli

         

Pick an account after Connect-MicrosoftTeams

4  Asked on November 4, 2021 by mauek-unak

   

Show local image file:///tmp/someimage.jpg

1  Asked on November 4, 2021 by dani-herrera

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir