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?
Asked
Active
Viewed 2,420 times
1 Answers
8
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
}
}
-
2But this is not the best answer, we have to tap for getting location, the true answer should work on hover! We should be able read location just with moving the mouse not moving and tapping! – ios coder Sep 29 '21 at 00:27