How to block NSView events under another NSView?

感情迁移 提交于 2019-12-05 22:12:51

问题


Here is the idea:

I have a NSWindow containing 2 NSView, let's call them ViewA and ViewB.

ViewA has a list of subview objects, each object has its own tracking area set and handles a mouseDown event. ViewB is a hidden view, which appears above ViewA.

The problem is when ViewB appears, ViewA still receives mouseDown events. So when I click on ViewB, the object behind the ViewB receives the mouseDown event. I would like to know if there's any way to block the events of ViewA while ViewB is over it.

I know I can remove the tracking area from every object, but it still responds to the mouseDown event.


回答1:


you can also disable the touch events for ViewA by [ViewA setAcceptsTouchEvents:NO]; and can enable them again as per your requirement by setting YES again.




回答2:


You can override sendEvent: method on NSWindow and test 'firstResponder', if it is ViewA, than not call [super sendEvent:event] so ViewA will not receive any event.




回答3:


If view B is a subview of A, the problem is that it's hidden. Don't hide it: just set its opacity to 0. That way you won't see it, but the responder chain will.




回答4:


In case anyone still looking for answer for these kind of questions nowadays, I only managed to do this with a child window, solution described in this accepted answer. Also, if you want to make the window transparent (/clear colored), but still receive mouse events on it, put this line into action as well:

[overlayWindow setIgnoresMouseEvents:NO];



回答5:


Sibling views block, descendant views dont as the child will propegate mouse events upstream to its parent. To block descendants propegating events to their parent you must overide the event in the child and not call super on the same event. Calling super will propegate the event to its parent. Here is a full explination on Events and hittesting sibling/descending views: (be warned its dense) http://stylekit.org/blog/2016/01/28/Hit-testing-sub-views/



来源:https://stackoverflow.com/questions/8858011/how-to-block-nsview-events-under-another-nsview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!