Can I prevent a DragGesture from triggering if it comes at the end of a LongPressGesture?

43 Views Asked by At

I have a parent view that has a DragGesture on it, and a lot of little child views in the parent view that have LongPressGestures on them. My problem is that when I long press on a child view, then move my finger, then release the child view, the parent view's DragGesture gets triggered.

I need the parent view's DragGesture not to be triggered if it comes right after a LongPressGesture from the child view.

Anyone have an idea for this?

Here is my code:

struct ParentView: View {
    
    private var children: [ChildView] = [ChildView(), ChildView(), ChildView()]
    
    var body: some View {
        
        VStack {
            ForEach(children, id:\.self) { _ in
                ChildView()
            }
        }
        .gesture(DragGesture()
            .onEnded({ _ in
                //drag action
            }))
    }
}

struct ChildView: View {
    
    @GestureState private var isDetectingLongPress = false
    
    var body: some View {
        HStack {
            //data
        }
        .gesture(LongPressGesture(minimumDuration: 1, maximumDistance: .infinity)
            .sequenced(before: LongPressGesture(minimumDuration: .infinity, maximumDistance: .infinity))
            .updating($isDetectingLongPress) { _,_,_ in
                //long press action
            })
    }
}
0

There are 0 best solutions below