I want to be able to use a different transition depending on the state transition. For example, if I go from .one => .three, I want to fade out ViewOne on removal. If I go from .one => .two, I want to move ViewOne to the left on removal. Right now I have something like this (which clearly, only works for a transition from .one => .two):
ZStack {
if state == .one {
ViewOne()
.transition(.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))
}
if state == .two {
ViewTwo()
.transition(.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))
}
if state == .three {
ViewThree()
.transition(.asymmetric(insertion: .opacity, removal: .identity))
}
}
How can I dynamically change the transition based on the state?
UPDATE: Here's a more complete example:
enum Screen {
case susi, onboarding, home
}
struct ContentView: View {
@State var screen: Screen = .susi
@State var transition: AnyTransition = .asymmetric(insertion: .identity, removal: .move(edge: .leading))
var body: some View {
ZStack {
if screen == .susi {
ViewOne()
.transition(transition)
}
if screen == .onboarding {
ViewTwo()
.transition(.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))
}
if screen == .home {
ViewThree()
.transition(.asymmetric(insertion: .opacity, removal: .identity))
}
}
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
withAnimation(.default) {
self.transition = .asymmetric(insertion: .identity, removal: .opacity)
self.screen = .home
}
}
}
}
}