NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Deprecated animation

Forums > SwiftUI

I followed a tutorial to get a nice animation with startup of the app. But the tutorial wasn't in ios15 or newer. So there was some depricated stuff in it like animation. One of them i got fiex but one keep getting problems with.

    var body: some View {
        VStack {
            ZStack {

                // Content
                if currentUserSignedIn {
                    tabView
                        .transition(.asymmetric(insertion: .move(edge: .bottom), removal: .move(edge: .top)))
                } else {
                    OnboardingView()
                        .transition(.asymmetric(insertion: .move(edge: .top), removal: .move(edge: .bottom)))
                }

                // Splash
                ZStack {
                    Color(color)

                    Image("JobIcon_white")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(
                            width: 85,
                            height: 85
                        )
                        .scaleEffect(animate ? 50 : 1)
                        .animation(.easeOut(duration: 0.5), value: 50)
                }
                .edgesIgnoringSafeArea(.all)
                .animation(.linear(duration: 0.3))
                .opacity(showSplash ? 1 : 0)
            }
        }
        .onAppear {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
                animate.toggle()
            }

            DispatchQueue.main.asyncAfter(deadline: .now() + 0.7) {
                showSplash.toggle()
            }
        }
    }

It's this line that's the problem: .animation(.linear(duration: 0.3))

   

@Bnerd  

if you have a boolean that controls the animation try the below

.animation(.linear(duration: 0.3), value: YourBoolean)

   

But when i add a bool the animation doesn't work anymore

   

@Bnerd  

Even if you toggle the boolean?

   

@maarten is wrestling with animation.

I think this animation doesn't work because you base the animation on a light switch that never changes.

TLDR: value: 50 doesn't change, so the animation doesn't fire.

@State private var shouldAnimate = true
Image("JobIcon_white")
    .scaleEffect(shouldAnimate ? 50 : 1)  // <- Choose 50 or 1 based on shouldAnimate boolean
    .animation(.easeOut(duration: 0.5), value: 50)  // <- Start the animation when value '50' changes
    // Plot twist!   The value never changes!

Try this?

@State private var shouldAnimate = true
Image("JobIcon_white")
    .scaleEffect(shouldAnimate ? 50 : 1)  // <- Choose 50 or 1 based on shouldAnimate boolean
    .animation(.easeOut(duration: 0.5), value: shouldAnimate)  // <- Start the animation when value shouldAnimate changes

   

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.