I made sample app with 3 pages and "Next", "Back" buttons.
When I click "Next" animation is perfect: the old content goes to the left and new content comes from the right.
However, I'm struggling to make "Back" animation to work nice simultaneously.
Goal: when clicking "Back" old content should go to the right and new content should come from the left. (animation for "Back" should be reverse to animation for "Next")
Any idea how to accomplish this?
Below is perfectly working transition for "Next"
struct ContentView: View {
@State var page: Int = 0
var body: some View {
VStack {
HStack {
Button(action: { withAnimation() { self.page = self.page - 1 } }) {
Text("Back")
}
Spacer()
Button(action: { withAnimation() { self.page = self.page + 1 }}) {
Text("Next")
}
}
Spacer()
if page == 0 {
PageView(name: "First page", color: .brown)
.transition(AnyTransition.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))
} else if page == 1 {
PageView(name: "Second page", color: .systemGreen)
.transition(AnyTransition.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))
} else if page == 2 {
PageView(name: "Third page", color: .systemBlue)
.transition(AnyTransition.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))
}
}
}
}
struct PageView: View {
var name: String
var color: UIColor
var body: some View {
HStack {
Spacer()
Text(name)
Spacer()
}
.padding()
.padding(.vertical, 50)
.background(Color(color))
}
}