The scenario is that, loading a 2nd ScrollView at a specific index when the user tapped on that index item in the 1st ScrollView.
Aka, selecting one image in 1st image list then open the 2nd image list with selected index image showed in full-screen.
How to do it in SwiftUI?
/ / / Here is the code.
private struct ImageList: View {
var images: [ImageViewModel]
@State private var showSheet = false
@State private var selectedImage = ImageViewModel.default
var body: some View {
VStack(alignment: .leading) {
Text("Images")
.font(.headline)
ScrollView(.horizontal) {
HStack(alignment: .top, spacing: 6) {
ForEach(images, id: \.self) { image in
KFImage(source: .network(image.fileURL))
.resizable()
.frame(width: 200)
.aspectRatio(1.77, contentMode: .fit)
.onTapGesture {
self.selectedImage = image
self.showSheet.toggle()
}
}
}.sheet(isPresented: $showSheet) {
PresentedImageList(images: self.images)
}
}.frame(height: 120)
}
.padding(.horizontal).padding(.bottom)
}
}
private struct PresentedImageList: View {
var images: [ImageViewModel]
var body: some View {
GeometryReader { gr in
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .top, spacing: 6) {
ForEach(self.images, id: \.self) { image in
KFImage(source: .network(image.fileURL))
.resizable()
.frame(width: gr.size.width)
.aspectRatio(1.77, contentMode: .fit)
}
}
}.background(Color.black)
}
}
}