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,
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,
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:
a good starting point, declared in NSResponder:
specifically, the button cell's container (not the cell itself) is the NSResponder.
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
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:
Here is i have created and worked for me perfectly...
Step 1: Create the Button with tracking area
Step: 2 Implement the following methods