Manage hit test mask in SwiftUI for an Image with transparency

Question says it all.

I want the transparent pixels to pass through the taps / clicks / gestures, while the opaque pixels catches them.

Obviously be able to control the behaviour would be even better, so I could ignore slightly translucent pixels too.

Pre-processing is not possible, user images, so it's not easy.

So far, the best I thought was to get a global gesture recognizer, and try to figure out where in my complex hierarchy this tap falls, and see if the image is underneath. But that seems overly complicated for something so simple and basic, really.

  • What you are trying is not so simple and basic even in UIKit. Can you share the complete code of your current (complicated) solution? Some readers might think of improvements if they could see the code.

  • When you say transparent, do you mean color is .clear ?

  • @Claude31 - I mean I have a 32-bit image with Alpha channel, so my pixels have (r,g,b,255) for fully opaque, and (r,g,b,0) for fully transparent. Since the image has transparency in it, tapping in a place where you see underneath is totally counter-intuitive.