如何使用 SwiftUI 获取鼠标位置?

How to get mouse location with SwiftUI?

我正在尝试在 SwiftUI 中显示弹出窗口。 我已经这样做了,但是有没有办法让我的 popoverView 出现在我点击的地方?为此,我想我需要获取鼠标位置。我该怎么做?

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 = [=10=].startLocation
        })
        
        // Spacers needed to make the VStack occupy the whole screen
        return VStack {
            
            Spacer()
            
            HStack {
                Spacer()
                Text("Tapped at: \(pt.x), \(pt.y)")
                Spacer()
            }
            
            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
    }
}