Clicking in SwiftUI View


Gerriet M. Denkmann
 

macOS 15.4

I have a SwiftUI ContentView, which contains a few vertically stacked Views.

For one of these I need the following functionality:
when the mouse moves inside the view, or when the view is clicked (or option clicked or whatsoever) I want to get notified of the mouse location in this view.

There is “onHover”, but this only tells me, whether the mouse has entered or left my view.

Then there is “onTapGesture”, which compiles, but never gets activated.

So: how to recognise mouse clicks in SwiftUI?

struct ContentView: View
{
VStack
{


ZStack
{
… draws a few Paths
}
.onTapGesture(perform: {print(#function)}) // never seen
.onHover()
{ pp in
print(“ ZStack \(pp ? "entered" : "left")")
}

...
}
}

Gerriet.

Join cocoa@apple-dev.groups.io to automatically receive all group messages.