SwiftUI获取默认键盘触摸的x和y坐标

问题描述

我想从键盘上触摸x和y坐标。我的代码在下面。

除了键盘外,我可以从屏幕上触摸到x和y坐标。 但是,我无法做到这一点。

我认为这是因为键盘不是UI部件。

每个人都知道吗?非常感谢!

import SwiftUI

struct ContentView: View {
    
    @State private var word: String = ""
    
    @State var xPos: CGFloat = 0
    @State var yPos: CGFloat = 0
    
    var body: some View {
        
        
        ZStack{
            Image(systemName: "")
                .resizable()
                .padding()
                .gesture(DragGesture(minimumdistance: 0,coordinateSpace: .global).onChanged {
                    dragGesture in
                    self.xPos = dragGesture.location.x
                    self.yPos = dragGesture.location.y
                    
                    print(self.xPos,self.yPos)
                    print(dragGesture.translation)
                }
                .onEnded {dragGesture in
                    self.xPos = dragGesture.location.x
                    self.yPos = dragGesture.location.y
                    print("Last",self.xPos,self.xPos)
                    print("Last",dragGesture.translation)
                })
            
            vstack{
                HStack{
                    Text("\(xPos)")
                    Text("\(yPos)")
                }
                vstack {
                    TextField("please write",text: $word)
                        .textFieldStyle(RoundedBorderTextFieldStyle())  
                        .padding()  
                    
                    Text("\(word)")
                    
                }
            }
        }
        
    }
    
}






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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)