4

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?

4

2 回答 2

2

这是我为我创造和工作的完美...

第 1 步:创建带有跟踪区域的 Button

 NSButton *myButton = [[NSButton alloc] initWithFrame:NSMakeRect(100, 7, 100, 50)];
[myButton setTitle:@"sample"];

[self.window.contentView addSubview:myButton];
// Insert code here to initialize your application
NSTrackingArea* trackingArea = [[NSTrackingArea alloc]
                                initWithRect:[myButton bounds]
                                options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways
                                owner:self userInfo:nil];
[myButton addTrackingArea:trackingArea];

步骤:2 实现以下方法

- (void)mouseEntered:(NSEvent *)theEvent{
NSLog(@"entered");
[[myButton cell] setBackgroundColor:[NSColor blueColor]];


}

- (void)mouseExited:(NSEvent *)theEvent{
[[myButton cell] setBackgroundColor:[NSColor redColor]];
NSLog(@"exited");

}
于 2014-02-26T18:48:07.973 回答
0

尝试

trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:options owner:self userInfo:nil];

代替

NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways;
    trackingArea = [[NSTrackingArea alloc] initWithRect:self.frame options:options owner:self userInfo:nil];
于 2012-10-26T09:40:46.100 回答