objective-c – 在NSView上使用NSPoint鼠标跟踪

我在App Delegate中有这个方法,它创建了一个窗口和内容视图,但我希望能够在输入和退出视图时使用NSPoint跟踪鼠标.问题是我不想创建一个NSView Custom类,并希望在我的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,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");
    }

}

解决方法

您应该可以使用 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
}

相关文章

我正在用TitaniumDeveloper编写一个应用程序,它允许我使用Ja...
我的问题是当我尝试从UIWebView中调用我的AngularJS应用程序...
我想获取在我的Mac上运行的所有前台应用程序的应用程序图标....
我是一名PHP开发人员,我使用MVC模式和面向对象的代码.我真的...
OSX中的SetTimer在Windows中是否有任何等效功能?我正在使用...
我不确定引擎盖下到底发生了什么,但这是我的设置,示例代码和...