I'm trying to complete button highlighted on mouse over event. So I subclassed NSButton
, where is put NSTrackingArea
and methods - (void)mouseEntered:(NSEvent *)event
and
- (void)updateTrackingAreas
.
Creation of the button looks so (it's in loop so I use array to collect):
CalendarTile *button = [[CalendarTile alloc] init];
[button setFrame:CGRectMake(point_x, point_y, button_frame_width, button_frame_height)];
[button setBordered:NO];
[button setBezelStyle:NSRegularSquareBezelStyle];
[button setButtonType:NSMomentaryChangeButton];
[button setFont:[NSFont fontWithName:@"Avenir Next" size:40]];
[button setAlignment:NSCenterTextAlignment];
[button setTitle:[NSString stringWithFormat:@"%i", i]];
[button setTextColor:[NSColor colorWithCalibratedRed:(float)62/255 green:(float)62/255 blue:(float)62/255 alpha:1.0]];
[arrayWithButtons addObject:button];
...
for (CalendarTile *btn in arrayWithButton) {
[self addSubview:btn];
}
And this is a subclass - CalendarTile.m:
@implementation CalendarTile
- (void)updateTrackingAreas
{
[super updateTrackingAreas];
if (trackingArea)
{
[self removeTrackingArea:trackingArea];
}
NSTrackingAreaOptions options = NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow;
trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:options owner:self userInfo:nil];
[self addTrackingArea:trackingArea];
}
- (void)mouseEntered:(NSEvent *)event
{
[self setImage:[NSImage imageNamed:@"highlight.png"]];
NSLog(@"HIGHLIGHT");
}
It should say in logs "HIGHLIGHT" when I have mouse over - it sadly doesn't.
Could you help me? What do I wrong?