【问题标题】:How to detect tap location in SwiftUI?如何检测 SwiftUI 中的点击位置?
【发布时间】:2021-05-01 12:34:05
【问题描述】:

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

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

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    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)
            }))
        }
    

    【讨论】:

    • 谢谢,它会打印点击位置。如何打印框架的宽度和高度。
    • UIScreen.main.bound.size.widthUIScreen.main.bound.size.height 将返回屏幕尺寸的宽度和高度
    猜你喜欢
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    相关资源
    最近更新 更多