我总是想用指尖触摸 x 和 y 坐标。
例如,我还想在使用另一个应用程序时获取坐标。所以我想在后台处理下面的代码。
在后台模式下使用后台处理似乎很好,但我不确定
大家有什么想法吗?非常感谢!
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()
}
}