4

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
        }
)
Marcy
  • 4,611
  • 2
  • 34
  • 52
Joseph
  • 691
  • 4
  • 18
  • Can u try wrapping the ```Component``` element in a ZStack. eg ```ZStack{Component(report: reports[i]).offset(y: -(CGFloat(i) * 12.0))} ``` – udi Feb 01 '23 at 12:54
  • @udi Thanks for the suggestion, but that doesn't achieve the desired result (still only prints when dragging horizontally) – Joseph Feb 01 '23 at 13:40
  • 1
    Depending on your problem, it might be easier (though not trivial) to track the content offset of your Scrollview. 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. – DatBlaueHus Feb 07 '23 at 08:35
  • @Joseph It would be really nice to get the translation for SwiftUI `ScrollView`s. I'm not sure if SwiftUI-Introspect may still work for this - if so, although also a bit hacky, it's a _possible_ solution. I've previously used it but it could change at any time after an iOS update. – George Feb 07 '23 at 16:07

2 Answers2

0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Joseph
  • 691
  • 4
  • 18
-1

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..

HannesH
  • 932
  • 2
  • 12