我正在尝试在SwiftUI中显示弹出窗口。 我已经准备好了,但是有没有办法使我的popoverView出现在我单击的位置?为此,我认为我需要获取鼠标位置。我该怎么办?
答案 0 :(得分:2)
在SwiftUILab@twitter的帮助下
struct ContentView: View {
@State private var pt: CGPoint = .zero
var body: some View {
let myGesture = DragGesture(minimumDistance: 0, coordinateSpace: .global).onEnded({
self.pt = $0.startLocation
})
// Spacers needed to make the VStack occupy the whole screen
return VStack {
Spacer()
Text("Tapped at: \(pt.x), \(pt.y)")
Spacer()
HStack { Spacer() }
}
.border(Color.green)
.contentShape(Rectangle()) // Make the entire VStack tappabable, otherwise, only the areay with text generates a gesture
.gesture(myGesture) // Add the gesture to the Vstack
}
}