Mouse events bleeding through NSView

I have an NSView that covers its view of the contents of the parent window. This view has a click event handler that removes it from the content view. Inside this point of view, I have a different point of view. When I drag the mouse in this internal view, the mouse events apply not only to the front view, but also to the rear views. In addition, the cursors from the views appear behind. This is the same problem that arises here: does the NSView overlay pass mouse events to the underlying subviews? but the answer there will not work for my project, because I cannot open another window.

+6
source share
2 answers

Without seeing the event processing code, it’s hard for you to understand what is happening, but I suspect that you can call super implementation of the various event processing methods in your implementations.

NSView is a subclass of NSResponder , so by default NSResponder events are sent to the responder chain. The view supervisor is the next object in the responder chain, so if you call, for example, [super mouseDown:event] in your implementation of ‑mouseDown: event will be passed to the supervisor.

The fix is ​​that you are not calling the super implementation in event handlers.

This is not true:

 - (void)mouseDown:(NSEvent*)anEvent { //do something [super mouseDown:event]; } 

It is right:

 - (void)mouseDown:(NSEvent*)anEvent { //do something } 
+13
source

Rob's answer and Maz’s comment on this answer solve this problem, but simply to make it absolutely clear. To prevent NSView from throwing mouse events to parents, empty methods must be implemented.

 // NSResponder ========================================= - (void) mouseDown:(NSEvent*)event {} - (void) mouseDragged:(NSEvent*)event {} - (void) mouseUp:(NSEvent*)event {} 
+2
source

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


All Articles