SWIFTUI - ANIMATED TRANSITIONS
I want to slide a new image from right to left when I click the next button and from left to right when I click the previous button. The sliding should be animated. Here is the code:
import SwiftUI
struct DetailView: View {
@State private var image: Image?
@State private var index: Int = 1
var body: some View {
VStack {
image?
.resizable()
.scaledToFit()
.transition(.slide)
.animation(.default)
}
.onAppear(perform:loadImage)
Button("Next", action: {
index += 1
loadImage()
})
Button("Previous", action: {
index -= 1
loadImage()
})
}
func loadImage() {
let strname = String(format: "image%02d", index)
image = Image (strname)
}
struct DetailView_Previews: PreviewProvider {
static var previews: some View {
DetailView()
}
}
}