我正试图完成鼠标悬停在事件上的按钮。所以我分类NSButton
,其中是NSTrackingArea
和方法- (void)mouseEntered:(NSEvent *)event
和 - (void)updateTrackingAreas
。按钮的NSButton NSTrackingArea - 跟踪不起作用
创建看起来如此(这是在循环,所以我用数组收集):
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];
}
这是一个子类 - 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");
}
应该说,在当鼠标悬停时记录“HIGHLIGHT” - 很遗憾没有。
你能帮我吗?我错了什么?
你确定你想用NSZeroRect初始化跟踪区吗? – TheAmateurProgrammer
好吧,我根据这个问题解决了我的问题: http://stackoverflow.com/questions/7889419/cocoa-button-rollovers-with-mouseentered-and-mouseexited –