我希望能够根据状态转换使用不同的转换。例如,如果我从 .one => .three 开始,我想在删除时淡出 ViewOne。如果我从 .one => .two 开始,我想在移除时将 ViewOne 移到左侧。现在我有这样的东西(很明显,它只适用于从 .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))
}
}
如何根据状态动态更改转换?
更新: 这是一个更完整的示例:
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
}
}
}
}
}