DragGesture on parent conflicts with child's LongPressGesture

I have a use case in which there is a zoomable and pannable parent view, and a child view that needs to display a custom context menu on long press.

(The reason why I need to implement a custom context menu is this: https://developer.apple.com/forums/thread/773810)

It seems, however, that this setup produces a bug in SwiftUI. The problem is that the onChanged of the drag gesture is only invoked right before its onEnded, hence you cannot smoothly animate the drag:

struct ContentView: View {
    var body: some View {
        ZStack {
            Rectangle()
                .foregroundStyle(.green)
                .frame(width: 200, height: 200)
                .onLongPressGesture {
                    print("long press")
                }
        }
        .gesture(MagnifyGesture().onEnded { value in
            print("magnify end")
        })
        .gesture(DragGesture()
            .onChanged { value in
                print("drag change")
            }
           .onEnded { value in
               print("drag end")
           })
    }
}

Changing the DragGesture to a .highPriorityGesture() makes the drag's onChange execute at the correct time but results in the LongPressGesture only triggering when the user lifts up his/her finger (which was originally not the case).

So it seems that the two gestures are in a sort of conflict with each other.

Is there a workaround?

You would need to either compose your gestures using Simultaneous gesture so that SwiftUI recognizes all subgesture patterns at the same time for it to recognize the combining gesture or Sequenced gestures so that SwiftUI recognizes each subgesture in order.

Checkout Composing SwiftUI gestures for examples on how to combine gestures

DragGesture on parent conflicts with child's LongPressGesture
 
 
Q