我正試圖完成鼠標懸停在事件上的按鈕。所以我分類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 –