I've been working on a GridView with programmatical scroll support, I've been using ScrollViewReader to programmatically scroll to the top & bottom of the GridView, but I have a trouble, I added two extra buttons to scroll 3 elements per touch, and it's working but there's new trouble with it.
If the user manually scrolls using the ScrollView, the ScrollViewReader shall not work, I mean, the extra buttons to scroll 3 elements per button touch shall not work ('cause the Index is not the current element on the Screen), I need to know the current element in the screen, or a better way to make my idea, here's my code
var body: some View {
ScrollView([.horizontal, .vertical]) {
ScrollViewReader { proxy in
Section(header: HeaderView(screenSize: $screenSize).border(Color("ApplicationTint"), width: 4)) {
ForEach(Elements.indices) { index in
Row(screenSize: $screenSize, elements: Elements[index])
.id(index)
Divider()
}
}
.onChange(of: viewModel.gridIndex, perform: { value in
withAnimation(.spring()) {
proxy.scrollTo(value, anchor: .leading)
}
})
}
}
Also, the buttons code.
Button {
viewModel.gridIndex = 0
} label: {
Image(systemName: "arrow.up.to.line")
}
Button {
viewModel.gridIndex -= 1
} label: {
Image(systemName: "arrow.up")
}
Button {
viewModel.gridIndex += 1
} label: {
Image(systemName: "arrow.down")
}
Button {
// viewModel.gridIndex = 0
viewModel.gridIndex = 0 + viewModel.totalgridIndex
} label: {
Image(systemName: "arrow.down.to.line")
}