1

I have a horizontal ScrollView and would like to provide a function that when the first element is not visible anymore an arrow should appear to signalize that there are some other elements. I've tried the DragGesture() to read the translation.width but it seems to be buggy in combination with the ScrollView.

So I'm looking for a way to detect whether the ScrollView was scrolled and how far. Is there any way?

private let gridItems = [GridItem(.flexible())]
ScrollView(.horizontal, showsIndicators: false) {
    HStack{
        LazyHGrid(rows: gridItems){
            ForEach(viewModel.imageOptions, id: \.self){ image in 
                ZStack {
                    Image(image)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 60, height: 60)
                        .background(Color.white)
                        .clipShape(Circle())
                        .background(
                            Circle()
                                .clipShape(Circle())
                                .shadow(color: Color.black.opacity(0.4), radius: 3, x: 5.0, y: 5.0)
                                )
                        .onTapGesture {
                        print("selected")
                        }
                        .padding(15)
                            
                    Circle()
                        .strokeBorder(Color.orange, lineWidth: 5)
                        .frame(width: 70, height: 70)
                        }
                }
            }
        }
    }
}
kirkyoyx
  • 313
  • 2
  • 12
  • Does this answer your question https://stackoverflow.com/a/62588295/12299030? – Asperi Jan 08 '21 at 06:41
  • Possible duplicate of this question: https://stackoverflow.com/questions/59342384/how-to-detect-scroll-direction-programmatically-in-swiftui-scrollview – SwiftySteve Jan 08 '21 at 09:05
  • No, I don't think the dragGesture is helpful because I need to check whether the ScrollView has been scrolled and how much the ScrollView has been scrolled. Since I can scroll though the whole ScrollView with one quick scroll or with multiple slow scrolls it's not helpful for my use case. – kirkyoyx Jan 08 '21 at 09:22

0 Answers0