如果直接重写 -(void)mouseMoved:(NSEvent *)theEvent和-(void)mouseExited:(NSEvent *)theEvent是没有用的,需要给NSView添加一个NSTrackingArea,像下面这样:
_trackingArea = [[NSTrackingArea alloc] initWithRect:[self frame]
options:NSTrackingMouseMoved+NSTrackingActiveInKeyWindow+NSTrackingMouseEnteredAndExited
owner:self
userInfo:nil];
[self addTrackingArea:_trackingArea];
[self becomeFirstResponder];
之后mouseMoved和mouseExited事件才会被执行。必需要options参数写上NSTrackingActiveInKeyWindow,否则会得到如下的错误
trackingArea options 0x2 do not specify when the tracking area is active