TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Adopting insertion and removal transition based on changes in observable value

Forums > SwiftUI

I came across an interesting issue today, that I am not able to figure out a sollution for. In essence I am working on a custom Tab View animation where existing and new views should slide out/in via transition, but depending on what new view is do so in a specific dirrections. i.e. say I have a 4 tabs in this order Home -> Account -> Settings

If user is on Home view and chooses Settings, Home view should slide to the left and Settings view should slide from right. Similarly if user is on Account view and chooses Home, Account view should slide to the right and Home view should slide in from left.

To acheive this, I created following component that wraps each Tab view

import SwiftUI

struct TabLayoutRoute<Content: View>: View {
  @ViewBuilder let content: Content

   private var isAnimateLeft: Bool {
    TabRouterBloc.view.rawValue > TabRouterBloc.previousView.rawValue
  }

  var body: some View {
    VStack {
      content
    }
    .transition(
      .asymmetric(
        insertion: .move(edge: isAnimateLeft ? .trailing : .leading),
        removal: .move(edge: isAnimateLeft ? .trailing : .leading)
      )
    )
  }
}

This creates a boolean isAnimateLeft that determines which move transition to apply based on observable value. I then wrap my views with this component and render them conditionally based on current TabRouterObservable.view in a parent with animation. Everything seems to work, but with issues. It feels like transitions are not reacting to changes in observable state i.e. they just use what was provided originally and don't update.

Is there a way to ensure that these transitions use latest values from observable?

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!

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.