在我的视图的"InitWithFrame"方法中,我正在设置一个跟踪区域,我想要捕获鼠标进入/退出事件.
我的问题有两个:
这是我初始化跟踪区域的方式:
trackingArea = [[NSTrackingArea alloc] initWithRect:rect
  options: (NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveAlways )
  owner:self userInfo:nil];
[self addTrackingArea:trackingArea];
任何线索为什么会发生这种情况?我希望只为我视图的一小部分(底部)调用鼠标进入/退出事件.
Mike Abdullah的回答解释了第2点.
以下是关于在不使用NSTrackingInVisibleRect标志时根本不接收事件的原因:
可能rect您提供的变量不在视图的坐标系中.您可以使用以下代码作为NSView子类的指定初始化程序来接收视图的整个区域的事件mouseEntered:和mouseExited:事件:
- (id)initWithFrame:(NSRect)frame 
{
    if ((self = [super initWithFrame:frame])) 
    {
        //by using [self bounds] we get our internal origin (0, 0)
        NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] options:(NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:self userInfo:nil];
        [self addTrackingArea:trackingArea];
        [trackingArea release];
    }
    return self;
}
Apple的文档说:
创建跟踪区域对象时,指定一个矩形(在视图的坐标系中),...
| 归档时间: | 
 | 
| 查看次数: | 1845 次 | 
| 最近记录: |