I am trying to make a popover display in SwiftUI. I already have this going, but is there a way to make my popoverView appear where I clicked? For this I think i need to get the mouse location. How do i do this?
With help from 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()
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With