NSButton 上的简单鼠标悬停效果
我正在为自定义渲染创建一个自定义 NSButtonCell
。
现在,我想要有不同的方面,具体取决于鼠标是否位于按钮上。我怎样才能得到这些信息?
谢谢和问候,
I am creating a custom NSButtonCell
for a custom rendering.
Now, I want to have different aspect depending if the mouse is over the button or not. How can I get this information?
Thanks and regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是我完美创建并为我工作的...
步骤 1:创建带有跟踪区域的按钮
步骤:2 实现以下方法
Here is i have created and worked for me perfectly...
Step 1: Create the Button with tracking area
Step: 2 Implement the following methods
Swift 3:
使用代码创建按钮或仅使用它的@IBOutlet。
然后定义鼠标悬停在按钮上的跟踪区域:
然后覆盖 mouseEntered 和 mouseExited,在这些函数中设置您想要更改的任何内容(按钮颜色、按钮图像、按钮文本等):
如果您有多个按钮(带有跟踪功能 )为每个按钮添加了区域)并且您需要确定哪个按钮触发了 mouseEntered 事件,您可以为此目的添加一些 userInfo 信息,因此不要:
在每个按钮的 userInfo 中添加您的自定义按钮名称,例如:
然后您可以编写switch-case 或 if mouseEntered 和 mouseExited 函数中的语句如下所示:
Swift 3:
Create the button with code or just use it's @IBOutlet.
Then define the button's tracking area for mouse over (hover):
Then override mouseEntered and mouseExited, set whatever you want to change (button color, button image, button text,..) in these functions:
If you have multiple buttons (with tracking area added for each) and you need to identify which button triggered the mouseEntered event, you can add some userInfo information for this purpose, so instead of:
Add your custom button name in userInfo for each button, for example:
Then you can write a switch-case or if statements in your mouseEntered and mouseExited functions like this:
您需要子类化 NSButton 类(或者更好的是 NSButtonCell 类)。
正如贾斯汀所说,如果你放置这两个方法,
当鼠标进入和退出该区域时,它们应该被调用。您可能还需要重新创建跟踪区域,请查看此处:
对于淡入和淡出效果,我使用了动画师和 alpha 值,例如:
You need to Subclass the NSButton class (or even better the NSButtonCell class).
As Justin said if you put the two method
They should get called when the mouse enter and exit the area. You may also need to re create the tracking area, look here:
For fade in and fade out effect I played with animator and alpha value for example:
带回调的 Swift 5 版本,超级好用:
用法:
Swift 5 version with callback, super easy to use:
Usage:
一个很好的起点,在 NSResponder 中声明:
具体来说,按钮单元格的容器(不是单元格本身)是 NSResponder。
a good starting point, declared in NSResponder:
specifically, the button cell's container (not the cell itself) is the NSResponder.
对于那些喜欢子类化的人,您还可以创建自己的
NSButton
并在其中分配NSTrackingArea
。这是一种非常简单而优雅的方法,感谢 Joey Zhou : https://github.com/Swift-Kit/JZHoverNSButton
它是用 Swift 2 编写的,但 XCode 会自动将其翻译为 Swift 3 - 4,无需任何操作问题。
希望它可以帮助别人
For those who prefer subclassing, you can also make your own
NSButton
and assigning theNSTrackingArea
in it.Here is a really simple and elegant way to do it, thanks to Joey Zhou : https://github.com/Swift-Kit/JZHoverNSButton
It is written in Swift 2 but XCode will automatically translate it in Swift 3 - 4 without any issue.
Hope it can help someone
这是一个简单的代码,用于跟踪某些控件(图像、标签等)上的
鼠标进入
和鼠标退出
事件:This is a simple code to track
mouse enter
andmouse exit
events on some controls (Images, Label, etc...):