如何在 SwiftUI 中同时识别 2 个拖动手势

问题描述 投票:0回答:1

我想同时识别两个拖动手势,以便我可以响应两指平移和放大手势。

struct GestureTest: View {
    @State var debug1 = ""
    @State var debug2 = ""
    
    var testGesture1: some Gesture {
        DragGesture(minimumDistance: 0.0)
            .onChanged { drag in
                debug1 = "test1: \(drag.location.debugDescription)"
            }
    }
    
    var testGesture2: some Gesture {
        DragGesture(minimumDistance: 0.0)
            .onChanged { drag in
                debug2 = "test2: \(drag.location.debugDescription)"
            }
    }
    
    var body: some View {
        Text(debug1)
        Text(debug2)
        Color.red
            .gesture(testGesture1)
            .gesture(testGesture2)
        Color.blue
            .gesture(testGesture1.simultaneously(with: testGesture2))
    }
}

我尝试使用 2 个独立手势(如

Color.red
所示),以及使用
.simultaneousGesture(_:)
修饰符(如
Color.blue
所示)。这两种方法都不起作用。我也尝试过使用 1
MagnifyGesture
MagnifyGesture.Value
没有任何东西可以检测平移手势。

有没有办法同时检测两个拖动手势,以便我可以响应 2 指平移手势以及放大手势?

ios swift swiftui gesture multi-touch
1个回答
0
投票

使用

MagnifyGesture

// Magnification Gesture
var magnify: some Gesture {
    MagnifyGesture()
        .onChanged { value in
            debug2 = "Magnify: \(value)"
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.