SwiftUI DragGesture swallowed by ScrollView
Asked Answered
N

0

6

I want to register a certain drag gesture on any SwiftUI view, including ScrollViews simultaneously with any other gestures (i.e. without influencing existing gestures). However, when adding a DragGesture on a ScrollView as follows, it seems like the gesture is immediately swallowed by the ScrollView.

ScrollView {
    Rectangle()
        .frame(width: 500, height: 500)
}
.simultaneousGesture(
    DragGesture()
        .onChanged { value in
            print("changed:", value.translation)
        }
        .onEnded { value in
            print("ended:", value.translation)
        }
)

When I drag the Rectangle, this code prints:

changed: (3.0, 16.666671752929688)

for example.

So onChanged is only called once, onEnded is never called.

Is there a way to make DragGestures work with ScrollViews as well?

Note:

I tried to find a workaround with GestureState and the updating(_:) modifier as well, but it didn't work either.

Nanna answered 25/4, 2022 at 9:47 Comment(1)
This kind of thing mostly is no possible, and maybe you could find a working code but at the end of the day you are going getting not in 100% working situations there would be always issues. The best way would be investing on a custom made scroll view, then you have full control.Gdynia

© 2022 - 2024 — McMap. All rights reserved.