In Cocoa, what is the best way to change the cursor when hovering over a circular view?

我是研究僧i 提交于 2019-12-12 14:59:04

问题


What I want to do

I have a circular custom NSView subclass, and I want to change the appearance of the cursor when the mouse is hovering over the circular portions of the view, but not when over the portions of the view's rectangle that fall outside the circle.

Here's an illustration. I would have inlined it with an image tag, but I'm too new to partake of such awesome features.

What I know so far

I know how to change the appearance of the cursor through NSCursor. I think that the best way to accomplish this for a rectangular view would be with a cursor rectangle. I know that I could receive mouseMoved events (and ought to turn them off when the mouse isn't over this view, using mouseEntered and mouseExited), and have a simple, inexpensive way to determine if a point lies in the region of interest.

So what's the problem then?

As far as I can tell, the system does not send mouseMoved events to a view that is not the first responder. Therefore, if I want to get mouseMoved events when the mouse is hovering over my view, I need to steal firstResponder status from whoever currently has it. If a text view has the focus, simply moving the mouse over such a view would steal it away, which is simply unacceptable from a usability standpoint.

Therefore, my question boils down to: is there a better way to do this? Can I get mouseMoved events without being the first responder?

Thanks!

I would have added the following tags: custom-views mouse-events NSCursor firstResponder But again, I'm an SA noob so I can't.


回答1:


I think you may just need to override the acceptsFirstResponder method in your NSView to return YES. If you don't, then the view won't receive any event information.




回答2:


Does it have to be a circular area? If anywhere within the rectangular bounds of the view is acceptable, you could use a cursor rect.



来源:https://stackoverflow.com/questions/1169639/in-cocoa-what-is-the-best-way-to-change-the-cursor-when-hovering-over-a-circula

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