DragGesture 正在取消 SwiftUI 中的 LongPressGesture

DragGesture is cancelling LongPressGesture in SwiftUI

我正在寻找的行为是长按一个小图片,它会变大,但松开后它应该会再次变小。

我能够成功完成此行为;但是,我遇到的问题是,在我按住之后,我将手指从图像上滑开,这样我就可以放大查看它,但它被检测为拖动手势,它取消了我的行为。

这是我的代码

import SwiftUI


struct ContentView: View {
    @GestureState private var isPressingDown: Bool = false

    @State private var width: CGFloat = 64
    @State private var height: CGFloat = 64

    var body: some View {
        let longPress = LongPressGesture(minimumDuration: 0.2)
        let infiniteLongPress = LongPressGesture(minimumDuration: .infinity)
        let seq = longPress.sequenced(before: infiniteLongPress)
        let upd = seq.updating($isPressingDown) { value, state, transaction in
            switch value {
                case .second(true, nil):
                    state = true
                default:
                    break
            }
        }

        return ZStack{

            Color.black
                .opacity(self.isPressingDown ? 0.5 : 0)
                .edgesIgnoringSafeArea(.all)
                .animation(.spring())


                Image("icon")
                    .resizable()
                    .frame(width:self.isPressingDown ? self.width * 5 : self.width, height:self.isPressingDown ? self.height * 5 : self.height, alignment:.center)
                    .gesture(upd)
                    .cornerRadius(10)
                    .animation(.spring(dampingFraction: 0.65))


        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

改为使用拖动手势作为第二个手势。测试并适用于 Xcode 11.2

...
let longPress = LongPressGesture(minimumDuration: 0.2)
let infiniteLongPress = DragGesture(minimumDistance: 0, coordinateSpace: .local) // here !
...

有时 Asperi 的解决方案对我不起作用,所以我用 highPriorityGesture 包围 'first gesture to receive interaction data',像这样:

...

.highPriorityGesture(
    LongPressGesture(minimumDuration: 0.5)
        .onEnded { _ in
            // do my stuff on 0.5 sec press         
        }
)

.gesture(
    DragGesture()
        .onEnded {
            // do my stuff on drag
     }
)

...

来自苹果的文档highPriorityGesture(_:including:):

Attaches a gesture to the view with a higher precedence than gestures defined by the view.