objective-c - 当鼠标在滚动时离开 trackingArea 时不会调用 mouseExited

标签 objective-c macos cocoa nstrackingarea

为什么当鼠标通过滚动或动画退出 NStrackingArea 时不调用 mouseExited/mouseEntered?

我创建这样的代码:

鼠标进入和退出:

-(void)mouseEntered:(NSEvent *)theEvent {
    NSLog(@"Mouse entered");
}

-(void)mouseExited:(NSEvent *)theEvent
{
    NSLog(@"Mouse exited");
}

跟踪区域:

-(void)updateTrackingAreas
{ 
    if(trackingArea != nil) {
        [self removeTrackingArea:trackingArea];
        [trackingArea release];
    }

    int opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways);
    trackingArea = [ [NSTrackingArea alloc] initWithRect:[self bounds]
                                             options:opts
                                               owner:self
                                            userInfo:nil];
    [self addTrackingArea:trackingArea];
}

更多详情:

我在 NSScrollView 的 View 中添加了 NSView 作为 subview 。每个 NSView 都有自己的跟踪区域,当我滚动我的 scrollView 并离开跟踪区域时,“mouseExited”没有被调用,但没有滚动一切正常。问题是当我滚动时调用“updateTrackingAreas”,我认为这会产生问题。

* 仅 NSView 存在同样的问题,但未将其添加为 subview ,因此这不是问题。

最佳答案

正如您在问题标题中指出的,仅当鼠标移动时才调用 mouseEntered 和 mouseExited。为什么会这样,我们先来看一下第一次添加NSTrackingAreas的过程。

举个简单的例子,让我们创建一个通常绘制白色背景的 View ,但如果用户将鼠标悬停在 View 上,它就会绘制红色背景。此示例使用 ARC。

@interface ExampleView

- (void) createTrackingArea

@property (nonatomic, retain) backgroundColor;
@property (nonatomic, retain) trackingArea;

@end

@implementation ExampleView

@synthesize backgroundColor;
@synthesize trackingArea

- (id) awakeFromNib
{
    [self setBackgroundColor: [NSColor whiteColor]];
    [self createTrackingArea];
}

- (void) createTrackingArea
{
    int opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways);
    trackingArea = [ [NSTrackingArea alloc] initWithRect:[self bounds]
                                             options:opts
                                               owner:self
                                            userInfo:nil];
    [self addTrackingArea:trackingArea];
}

- (void) drawRect: (NSRect) rect
{
    [[self backgroundColor] set];
    NSRectFill(rect);
}

- (void) mouseEntered: (NSEvent*) theEvent
{
    [self setBackgroundColor: [NSColor redColor]];
}

- (void) mouseEntered: (NSEvent*) theEvent
{
    [self setBackgroundColor: [NSColor whiteColor]];
}

@end

这段代码有两个问题。首先,当调用 -awakeFromNib 时,如果鼠标已经在 View 内,则不会调用 -mouseEntered。这意味着即使鼠标悬停在 View 上,背景仍然是白色的。这实际上在 NSView 文档中提到了 -addTrackingRect:owner:userData:assumeInside 的 assumeInside 参数:

If YES, the first event will be generated when the cursor leaves aRect, regardless if the cursor is inside aRect when the tracking rectangle is added. If NO the first event will be generated when the cursor leaves aRect if the cursor is initially inside aRect, or when the cursor enters aRect if the cursor is initially outside aRect.

在这两种情况下,如果鼠标在跟踪区域内,则在鼠标离开跟踪区域之前不会生成任何事件。

所以要解决这个问题,当我们添加跟踪区域时,我们需要找出光标是否在跟踪区域内。我们的 -createTrackingArea 方法因此变成了

- (void) createTrackingArea
{
    int opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways);
    trackingArea = [ [NSTrackingArea alloc] initWithRect:[self bounds]
                                             options:opts
                                               owner:self
                                            userInfo:nil];
    [self addTrackingArea:trackingArea];

    NSPoint mouseLocation = [[self window] mouseLocationOutsideOfEventStream];
    mouseLocation = [self convertPoint: mouseLocation
                              fromView: nil];

    if (NSPointInRect(mouseLocation, [self bounds]))
    {
        [self mouseEntered: nil];
    }
    else
    {
        [self mouseExited: nil];
    }
}

第二个问题是滚动。当滚动或移动 View 时,我们需要重新计算该 View 中的 NSTrackingAreas。这是通过删除跟踪区域然后将它们添加回去来完成的。正如您所注意到的,-updateTrackingAreas 在您 ScrollView 时被调用。这是删除和重新添加区域的地方。

- (void) updateTrackingAreas
{
    [self removeTrackingArea:trackingArea];
    [self createTrackingArea];
    [super updateTrackingAreas]; // Needed, according to the NSView documentation
}

这应该可以解决您的问题。诚然,每次添加跟踪区域时都需要找到鼠标位置然后将其转换为 View 坐标,这很快就会变得陈旧,因此我建议在 NSView 上创建一个自动处理此问题的类别。您不会总是能够调用 [self mouseEntered: nil] 或 [self mouseExited: nil],因此您可能希望让类别接受几个 block 。如果鼠标在 NSTrackingArea 中,则运行一个,如果不在,则运行一个。

关于objective-c - 当鼠标在滚动时离开 trackingArea 时不会调用 mouseExited,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8979639/

相关文章:

macos - gcc 11.3.0 on macOS/Apple Silicon 和 SHA-3 指令

java - 如何使用 Aqua/system LAF 让 JPanel 使用没有选项卡的 JTabbedPane UI?

objective-c - cocoa 事件水龙头

cocoa - 双击 Cocoa 中的 NSTableView 行?

objective-c - NSFullSizeContentViewWindowMask 和标题/工具栏高度?

iphone - Objective C,内存管理

objective-c - 当多个字段开始编辑时更改 UITextField 背景

Objective-C 中指针的使用

ios - 如何获得 linkedin 连接?

objective-c - [__NSCFConstantString pointSize] : unrecognized selector sent to instanc