How to detect mouse hover on NSButton. Obj-C, Cocoa

I have a Mac application in which there are two NSButtons in each bottom corner of the window. I set them to Alpha = .5 in .xib. I want to know how to detect mouse hover over a button and change alpha so that it can be said .9. How can I do it? I suppose all you need to know from my code is .h.

 @property (unsafe_unretained) IBOutlet NSButton *leftButton; @property (unsafe_unretained) IBOutlet NSButton *rightButton; 

They are synthesized in my .m. The deployment objective is OS X 10.6+. Thank you

EDIT

I really haven't tried anything. I saw something on NSTrackingArea, but not sure how to use it, more importantly, I was wondering if there is another way. For example, an event that has already been triggered or something else.

+4
source share
1 answer

You need to subclass the NSButton class (or even better the NSButtonCell class).

 - (void)mouseEntered:(NSEvent *)theEvent; - (void)mouseExited:(NSEvent *)theEvent; 

They should be called when the mouse enters and leaves the area. You may also need to create a tracking zone, see here:

 - (void)updateTrackingAreas 

For the fade and fade effect, I played with an animator and an alpha value, for example:

 [[self animator]setAlphaValue:0.9]; 

EDIT: This is just for reference, so you can take some ideas

 @interface MyButton : NSButton { - (void)mouseEntered:(NSEvent *)theEvent; - (void)mouseExited:(NSEvent *)theEvent; - (void)updateTrackingAreas; @end 
+5
source

Source: https://habr.com/ru/post/1447229/


All Articles