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

标签 objective-c xcode cocoa

我在 App Delegate 中有这个方法,它创建一个窗口和内容 View ,但我希望能够在进入和退出 View 时使用 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");
    }

}

最佳答案

你应该能够使用 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
}

关于objective-c - 在 NSView 上使用 NSPoint 鼠标跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9936794/

相关文章:

iphone - 混合 CATextLayer 和 CAScrollLayer

c++ - Xcode 10 调用不可用的函数 std::visit

ios - 强制 Xcode 在构建时始终复制资源文件夹

objective-c - 无法在 Cocoa 中找到正在运行的应用程序

cocoa - 复制 NSTextView 文本

objective-c - 在桌面项目上使用 autosizing 或 autoresizingMask 不好吗?

ios - 键盘隐藏了 IOS 中的文本字段

ios - 放松 segue 不工作

iphone - 尝试在NSUserDefaults中保存索引路径时出现错误

ios - 使用 Swift 代码的应用程序在部署后启动时崩溃