问题描述
在视图的InitWithFrame方法中,我设置了一个要捕获鼠标进入/退出事件的跟踪区域。
我的问题有两个:
In my "InitWithFrame" method of a view I'm setting a tracking area for which I want to capture mouse enter/exit events.
My problems are two fold:
- 如果没有NSTrackingInVisibleRect,事件将不会被调用。
- 无论使用什么rect覆盖整个视图的框架或仅占据其一小部分的框架 - 无论鼠标光标在视图上的位置,都为整个视图调用鼠标进入/退出事件。
这是我如何初始化跟踪区域:
this is how I initialize the tracking area:
trackingArea = [[NSTrackingArea alloc] initWithRect:rect
options: (NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveAlways )
owner:self userInfo:nil];
[self addTrackingArea:trackingArea];
任何线索为什么会发生这种情况?
Any clues why this happens? I want the mouse enter/exit events to be called only for a small portion (the bottom part) of my view.
推荐答案
我想要鼠标进入/退出事件只对我的视图的一小部分
Mike Abdullah's answer explains point 2.
这里是一个猜测,为什么不使用NSTrackingInVisibleRect标志时根本没有收到事件:
可能您提供的变量 rect
不在视图的坐标系内。您可以使用以下代码作为NSView子类的指定初始化程序,以接收 mouseEntered:
和 mouseExited:
Here is a guess about why you don't receive events at all when not using the NSTrackingInVisibleRect flag:
Probably the variable rect
you provide is not within the view's coordinate system. You could use the following code as the designated initializer of your NSView subclass to receive mouseEntered:
and mouseExited:
events for the whole area of your view:
- (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的说:
这篇关于NSTrackingArea工作奇怪 - 整个视图,或没有什么...没有矩形尊重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!