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

Top level animation and transition view modifier affecting subviews

Forums > SwiftUI

Hi,

I am a bit confused as to how to approach this problem:

Suppose I have this kind of view:

struct RootView: View {
  @State private var isFirstLaunch: Bool = true

  @ViewBuilder var body: some View {
     if isFirstLaunch {
       FirstLaunchView(isFirstLaunch: $isFirstLaunch) // to change this once done
     } else {
       MainAppView()
     }
   }
}

Note: this is just an example, usually I'd persist the firstLaunch flag.

So I want to display a first launch view on the first launch, otherwise the app itself. This works nicely and now I want to work on transitioning between those states:

FirstLaunchView(isFirstLaunch: $isFirstLaunch)
    .transition(.move(edge: .leading))
    .animation(Animation.easeOut(duration: 0.6))
MainAppView()
    .transition(.move(edge: .trailing))
    .animation(Animation.easeOut(duration: 0.6))

Sort of like a paging animation. This also works great but here is what happens:

In my FirstLaunchView I transition between the first launch pages (Welcome, Tutorial, etc.). I make use of .transition and .animation view modifiers as well and they look amazing. The only problem arises is that the top level view modifiers for transitioning between the views themselves messes up the animation in the views below like this:

FirstLaunchView(...).transition(.move(edge: .leading)).animation(Animation.easeOut(duration: 0.6)

turns into:

... pageView(for page: currentPage).transition(.slide).animation(.default).transition(.move(edge: .leading)).animation(Animation.easeOut(duration: 0.6))

and therefore the animations of the "subviews" in first launch are affected by the top level animations of the view transition.

Is there any way that I can make animation only apply when actually transitioning?

How can I animate and transition subviews while applying a different animation to a top level view? Thanks alot!

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.