I can't make insertion transition to work in SwiftUI.
I have a Group
which conditionaly displayes one of two views. When I'm trying to animate the transition, removal transition works but insertion doesn't - the view just appears right away without any animation.
I'm pasting below my view code. How can I make this work? (Xcode 11.3.1)
struct TestView: View {
@State private var showView = false
var body: some View {
VStack {
Button(action: {
withAnimation {
self.showView.toggle()
}
}) {
Text("Tap")
}
Group {
if showView {
Color.red
.frame(width: 100, height: 100)
} else {
Color.blue
.frame(width: 100, height: 100)
.cornerRadius(50)
}
}
.transition(.asymmetric(insertion: .move(edge: .leading),
removal: .move(edge: .trailing)))
}
}
}
Edit:
As @Asperi pointed out in the comment, the code is correct but... it will only work when run on real device. Live previews in Xcode are buggy and apparently doesn't handle transitions very well.
So the answer for this question is simple: test on real device! :)
Well, ok, it is just observation from the SwiftUI Preview history and until now (can't say what will be in the next version), but - transitions do not work properly in Preview at all (static or Live - it looks like limitation, so just don't test them there.
Test transitions either on standalone Simulator or, what is preferable, on Real Device.