How to achieve mouse click on NSView using CALayer?

I have a custom NSWindow without borders in the shape of a circle. To do this, I use a custom NSView that draws a filled circle.

The mouse clicks on a transparent pixel inside the contents of the window directly through the window so that any application is behind it. Clicks inside the filled circle are intercepted by my application. So far so good.

The problem is that in the next line, the above behavior changes and all clicks inside the contents of the Windows content are intercepted, a transparent pixel or not:

[circleView setWantsLayer:YES]; 

Is there a way for clicks on the transparent pixels of CALayer instances CALayer pass through the window / view / layer into the application behind it?

This is essentially the same as described below (only that the solution does not work in all cases, because it does not support animation):

http://www.cocoabuilder.com/archive/cocoa/235281-clicking-through-nsview-with-calayers.html

+4
source share
2 answers

I'm not quite sure if this works on layer-supported views, but a possible solution is to override - (NSView *)hitTest:(NSPoint)aPoint in a subclass of the circle view and check if aPoint inside the circle path. If it is outside the circle, returning nil from this method should cause the click to pass.

0
source

CAShapeLayer with CGPath will work, or you can create a CAShapeLayer as a mask for the content level.

0
source

Source: https://habr.com/ru/post/1344613/


All Articles