如何检测用户何时打开OS X Mountain Lion通知中心?
是否有一个NSNotification(呃,非常相似的术语表示不同的东西),我可以观察到它?
我不知道有没有官方记录的解决方案或通知(请告诉我!),但在我测试时,这似乎有效(至少在OS X 10.10上),只要我的应用程序在前台/具有最前面的窗口。
将您的对象添加为观察者:
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationCenterOpened:) name:@"com.apple.HIToolbox.beginMenuTrackingNotification" object:nil];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationCenterClosed:) name:@"com.apple.HIToolbox.endMenuTrackingNotification" object:nil];
ToolboxMessageEventData
编号(4927
),例如:- (void)notificationCenterOpened:(NSNotification*)notification {
if ([notification.userInfo[@"ToolboxMessageEventData"] isEqual: @4927]) {
NSLog(@"Notification center opened");
}
}
- (void)notificationCenterClosed:(NSNotification*)notification {
if ([notification.userInfo[@"ToolboxMessageEventData"] isEqual: @4927]) {
NSLog(@"Notification center closed");
}
}
请告诉我这对您是否奏效。
没关系 - 在重新启动/注销 + 重新登录后,ToolboxMessageEventData似乎会改变。
[[NSScreen mainScreen] visibleFrame]
来实现,但这似乎无法调整通知中心抽屉的高度(OS X 10.9)。 - pkamb