【发布时间】:2012-09-15 09:50:12
【问题描述】:
我正在尝试完成在鼠标悬停事件上突出显示的按钮。所以我将NSButton 子类化,NSTrackingArea 和方法- (void)mouseEntered:(NSEvent *)event 和
- (void)updateTrackingAreas.
按钮的创建看起来是这样(它在循环中,所以我使用数组来收集):
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 初始化跟踪区域吗?
-
好的,我按照这个issue解决了我的问题:stackoverflow.com/questions/7889419/…
标签: objective-c xcode macos cocoa nsbutton