skip to Main Content

I am trying to get the position of the finger during a drag gesture (from an origin point of where the finger first began the drag). I want to do this while simultaneously allowing for scrolling within a ScrollView.

I can do these independently, but it seems scrolling in a ScrollView prevents the drag gesture.

I have tried adding the .gesture() part of the code below to the Component(), the VStack, and I also tried wrapping all of this in a GeometryReader and adding it to that. Each time, the .gesture() only prints when no scrolling happens (so, it prints when dragging horizontally).

How can I achieve this?

ScrollView {
    VStack(alignment: .center, spacing: 0) {
        ForEach((0..<reports.count), id: .self) {i in
            Component(report: reports[i])
                .offset(y: -(CGFloat(i) * 12.0))
        }
    }
}
.gesture(
    DragGesture()
        .onChanged { value in
            self.position = value.translation
            print(self.position)
        }
        .onEnded { _ in
            self.position = .zero
        }
)

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to everyone for the suggestions (some of which were deleted by their authors). Unfortunately, as DatBlaueHus states:

    Unfortunately, the Scrollview drag handler kills all simultaneous drag handlers as well, so afaik there is no stable solution for your original issue short of wrapping a UIKit solution for that issue.

    I've solved my issue by using a custom ScrollView implementation. I need to play around with the feel of it — it doesn't scroll quite as smoothly as the stock ScrollView, but it solves my issue and recognises gestures simultaneously.


  2. this answer might work, but it also seems like a weird workaround.

    worst case you must build your own scrollview, as to have access to all touches and then offsetting stuff so scroll also works..

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search