在NSView上使用NSPoint鼠标跟踪 [英] Using NSPoint Mouse Tracking on NSView

查看:171
本文介绍了在NSView上使用NSPoint鼠标跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在App Delegate中拥有此方法,该方法可以创建窗口和内容视图,但是我希望能够使用NSPoint在进入和退出视图时跟踪鼠标。问题是我不想创建一个NSView Custom类,并且想要在我的AppDelegate中完成所有操作。鼠标跟踪(在底部)不起作用,有什么建议吗?

I have this method in App Delegate that makes a window and content view but I want to be able to track the mouse using an NSPoint on entered and exiting the view. The problem is I do not want to make a NSView Custom class and want to do it all within my AppDelegate. The mouse tracking (at the bottom) does not work, any suggestions?

- (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 ;您可以执行以下操作(在浏览器中输入,未经测试):

You should be able to do this using NSTrackingArea; You'd do something like this (typed in the browser, not tested):

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
}

这篇关于在NSView上使用NSPoint鼠标跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆