如何在 SwiftUI 中检测点击位置?

时间:2021-05-01 12:34:05

标签: swiftui

我想打印 <#code#> 块中点击点的位置(x,y 坐标)以及我的视图尺寸。

struct ContentView: View {
    var body: some View {
        self.onTapGesture {
            <#code#>
        }
    }
}

1 个答案:

答案 0 :(得分:3)

DragGestureminimumDistance: 0 添加到您的身体中,例如:

var body: some View {
        VStack
        {
            
        }
        .frame(
            minWidth: 0,
            maxWidth: .infinity,
            minHeight: 0,
            maxHeight: .infinity,
            alignment: .topLeading
        )
        .background(Color.red)
        .gesture(DragGesture(minimumDistance: 0).onEnded({ (value) in
            print(value.location)
        }))
    }