Adding a drag gesture in SwiftUI to a View inside a ScrollView blocks the scrolling

Solution 1:

You can set minimumDistance to some value (for instance 30). Then the drag only works when you drag horizontally and reach the minimum distance, otherwise the scrollview or list gesture override the view gesture

.gesture(DragGesture(minimumDistance: 30, coordinateSpace: .local)

Solution 2:

Just before

.gesture(drag)

You can add

.onTapGesture { }

This works for me, apparently adding a tapGesture avoids confusion between the two DragGestures.

I hope this helps

Solution 3:

I have created an easy to use extension based on the Michel's answer.

struct NoButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
    }
}

extension View {
    func delayTouches() -> some View {
        Button(action: {}) {
            highPriorityGesture(TapGesture())
        }
        .buttonStyle(NoButtonStyle())
    }
}

You apply it after using a drag gesture.

Example:

ScrollView {
    YourView()
        .gesture(DragGesture(minimumDistance: 0)
            .onChanged { _ in }
            .onEnded { _ in }
        )
        .delayTouches()
}