UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

2      

@Bnerd  

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

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

2      

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

2      

@Bnerd  

Even if you toggle the boolean?

2      

@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

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.