2

我正在为应该控制 iTunes 的 Mac 开发 Today Extension。它可以正常工作,但我的 UI 包含一个方形封面艺术图像,上面覆盖着效果视图,其中包含元数据和控件:

德普

现在,很明显,如果我能以某种方式让它们仅在需要时出现——比如说,当鼠标光标位于我的扩展程序视图上方时,那就太好了。

正如我之前处理过这类事情一样,我决定将一个小子NSView类放在一起,用于NSTrackingArea在鼠标进入或存在其边界时触发通知:

/**
 * Sets up the tracking area, for the entire bounds of the view.
 */
- (void) setUpTrackingRect {
    _trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds
                                                 options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect
                                                   owner:self
                                                userInfo:nil];

    [self addTrackingArea:_trackingArea];
}

/**
 * Mouse entry: send TSMouseTrackingViewMouseEntered notification.
 */
- (void) mouseEntered:(NSEvent *) theEvent {
    NSLog(@"Mouse Enter");
    [[NSNotificationCenter defaultCenter] postNotificationName:TSMouseTrackingViewMouseEntered
                                                        object:self];
}

/**
 * Mouse exit: send TSMouseTrackingViewMouseLeft notification.
 */
- (void) mouseExited:(NSEvent *) theEvent {
    NSLog(@"Mouse Exit");
    [[NSNotificationCenter defaultCenter] postNotificationName:TSMouseTrackingViewMouseLeft
                                                        object:self];
}

(摘自我在 GitHub 上的完整代码。

这些NSLogs 可以帮助我进行调试:尽管我的光标毫无问题地移入和移出视图,但它们永远不会触发。

我一直在查看 Apple 的文档,但找不到任何明确禁止此类事情的内容,或者解释为什么它不起作用。它是一个标准的NSView子类,在一个 中NSViewController,但显示在通知中心,而不是一个独立的应用程序。

任何关于为什么这个简单的跟踪区域视图在 Today Extension 中不起作用的建议都值得赞赏。

4

0 回答 0