NSView mouse tracking

这一生的挚爱 提交于 2021-02-20 09:56:00

问题


I'm facing a strange behavior with Cocoa NSView on Mac OS X.

I've a custom NSView in a NSView container, this custom NSView tracks mouse movements, clicks, and has a tooltip. When I add a NSView above the described view, I can still see the tooltips even if the view with the tooltip is under, behind and not visible.

I'm pretty sure that I misunderstood something in the event handling chain. Any help is really appreciated!


回答1:


The core issue is that you are not supposed to have overlapping views in Cocoa. Or at least, the behavior then becomes undefined. A view can be a subview of another view, but not simply a sibling within the bounds of the other view.

However, one way to solve your particular problem is to make the view underneath hidden, using the setHidden: method.




回答2:


If you're not using it anymore you can call the removeFromSuperview method.

NSView *myView
[myView alloc] init]
// do stuff
[myView removeFromSuperview]

if you just don't want it to receive events you can call the resignFirstResponder method

NSView *myView
[[myView alloc] init] 
// do stuff
[myView resignFirstResponder]


来源:https://stackoverflow.com/questions/9225573/nsview-mouse-tracking

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