如何解决SwiftUI获取默认键盘触摸的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 举报,一经查实,本站将立刻删除。