我有一个仅状态栏项目的应用程序,我试图在 mouseOver 上显示一个面板。我已经连接并工作了自定义状态项(和相关视图),但跟踪矩形仅在每十几个启动时接收事件。这使我相信某处发生了竞争情况,但我找不到。在我的自定义状态栏项目视图中:
- (id)initWithStatusItem:(NSStatusItem *)statusItem {
CGFloat itemWidth = [statusItem length];
CGFloat itemHeight = [[NSStatusBar systemStatusBar] thickness];
NSRect itemRect = NSMakeRect(0.0, 0.0, itemWidth, itemHeight);
NSLog(@"itemRect: %@", NSStringFromRect(itemRect));
if ((self = [super initWithFrame:itemRect])) {
_statusItem = statusItem;
_statusItem.view = self;
NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveAlways;
NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:itemRect
options:options
owner:self
userInfo:nil];
[self addTrackingArea:trackingArea];
[self.window setIgnoresMouseEvents:NO];
[self.window setAcceptsMouseMovedEvents:YES];
self.wantsLayer = YES;
}
return self;
}
- (void)mouseEntered:(NSEvent *)theEvent {
[[NSNotificationCenter defaultCenter] postNotificationName:UAStatusItemMouseEnteredNotification object:nil];
}
- (void)mouseExited:(NSEvent *)theEvent {
[[NSNotificationCenter defaultCenter] postNotificationName:UAStatusItemMouseExitedNotification object:nil];
}
在大多数启动时,应用程序不会响应跟踪鼠标事件,但每隔一段时间,mouseEntered:
和mouseExited:
方法就会被正确调用,这让我完全困惑。这里发生了什么,我做错了什么?
编辑 07/17/2012
我根据@Streams 的回答更改了代码,但看到了同样的问题:
- (id)initWithStatusItem:(NSStatusItem *)statusItem {
CGFloat itemWidth = [statusItem length];
CGFloat itemHeight = [[NSStatusBar systemStatusBar] thickness];
NSRect itemRect = NSMakeRect(0.0, 0.0, itemWidth, itemHeight);
NSLog(@"itemRect: %@", NSStringFromRect(itemRect));
if ((self = [super initWithFrame:itemRect])) {
_statusItem = statusItem;
_statusItem.view = self;
[self updateTrackingAreas];
[self.window setIgnoresMouseEvents:NO];
[self.window setAcceptsMouseMovedEvents:YES];
self.wantsLayer = YES;
}
return self;
}
- (void)updateTrackingAreas {
if (self.trackingArea)
[self removeTrackingArea:self.trackingArea];
[super updateTrackingAreas];
self.trackingArea = [[NSTrackingArea alloc] initWithRect:CGRectZero
options:NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingInVisibleRect | NSTrackingActiveAlways
owner:self
userInfo:nil];
[self addTrackingArea:self.trackingArea];
}
编辑 07/18/2012
这是一个准系统示例项目,它使用众所周知的 github 项目(由 @Stream 编写)来显示问题。它不能可靠地接收鼠标悬停事件,如果有的话。