在NSView上使用NSPoint鼠标跟踪

4

我在App Delegate中有一个方法,用于创建窗口和内容视图,但我想能够在进入和退出视图时使用NSPoint跟踪鼠标。问题是我不想创建一个NSView自定义类,而是希望在我的AppDelegate中完成所有操作。底部的鼠标跟踪(参考代码)无法正常工作,请问是否有建议?

- (void)toggleHelpDisplay
{
        // Create helpWindow.
        NSRect mainFrame = [[NSScreen mainScreen] frame];
        NSRect helpFrame = NSZeroRect;
        float width = 75;
        float height = 75;
        helpFrame.origin.x = (mainFrame.size.width - width) / 2.0;
        helpFrame.origin.y = 200.0;
        helpFrame.size.width = width;
        helpFrame.size.height = height;
        helpWindow = [[BrightnessView windowWithFrame:helpFrame] retain];

        // Configure window.
        [helpWindow setReleasedWhenClosed:YES];
        [helpWindow setHidesOnDeactivate:NO];
        [helpWindow setCanHide:NO];
        [helpWindow setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
        [helpWindow setIgnoresMouseEvents:NO];

        // Configure contentView.
        NSView *contentView = [helpWindow contentView];
        [contentView setWantsLayer:YES];
        CATextLayer *layer = [CATextLayer layer];
        layer.opacity = 0;
        [contentView setLayer:layer];
        CGColorRef bgColor = CGColorCreateGenericGray(0.0, 0.6);
        layer.backgroundColor = bgColor;
        CGColorRelease(bgColor);
        layer.string = ? HELP_TEXT : HELP_TEXT_OFF;
        layer.contentsRect = CGRectMake(0, 0, 1, 1.2);
        layer.fontSize = 40.0;
        layer.foregroundColor = CGColorGetConstantColor(kCGColorWhite);
        layer.borderColor = CGColorGetConstantColor(kCGColorWhite);
        layer.borderWidth = 4.0;
        layer.cornerRadius = 4.0;
        layer.alignmentMode = kCAAlignmentCenter;

        [window addChildWindow:helpWindow ordered:NSWindowAbove];

        float helpOpacity = (([NSApp isActive] ? 1 : 0));
        [[[helpWindow contentView] layer] setOpacity:helpOpacity];

        //track mouse so that once hovered make larger.
    NSPoint mouseLocation = [[self window] mouseLocationOutsideOfEventStream];

    if (NSPointInRect(mouseLocation, helpFrame)) {
        NSLog(@"mouse over");
    }
    else 
    {
        NSLog(@"mouse not over");
    }

}
1个回答

9

您可以使用NSTrackingArea来完成此操作;您可以像这样操作(在浏览器中输入,未经测试):

self.helpView = contentView; // Need to store a reference to the view if you want to convert from its coordinate system
// Set up a tracking area
NSTrackingArea *trackingArea = [[[NSTrackingArea alloc] initWithRect:[self.helpView bounds]
                                                             options:NSTrackingActiveInActiveApp | NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved
                                                               owner:self
                                                            userInfo:nil] autorelease];
[self.helpView addTrackingArea:trackingArea];

- (void)mouseEntered:(NSEvent *)event;
{
    NSPoint location = [self.helpView convertPoint:[event locationInWindow] fromView:nil];
    // Do whatever you want to do in response to mouse entering
}

- (void)mouseExited:(NSEvent *)event;
{
    NSPoint location = [self.helpView convertPoint:[event locationInWindow] fromView:nil];
    // Do whatever you want to do in response to mouse exiting
}

- (void)mouseMoved:(NSEvent *)event;
{
    NSPoint location = [self.helpView convertPoint:[event locationInWindow] fromView:nil];
    // Do whatever you want to do in response to mouse movements
}

1
除了我收到错误跟踪区域选项0x3未指定跟踪区域处于活动状态以外,一切似乎都正常运作。有什么想法吗?谢谢! - Grant Wilkinson
我们为什么需要一个辅助视图?为什么NSView所有者要独立跟踪鼠标? - Charlton Provatas
算了,原来你根本不需要辅助视图。 - Charlton Provatas
1
@CharltonProvatas 这不是一个“helper view”,我只是将视图命名为helpView,因为提问者说它是一个帮助窗口。任何NSView都可以使用 :) - Andrew Madsen
@AndrewMadsen 我现在明白了。一开始我只是有点困惑是否需要有第二个视图来进行鼠标跟踪。结果发现,如果愿意的话,一个 NSView 所有者也可以进行鼠标跟踪 :D - Charlton Provatas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接