The owner of an NSTrackingArea object created with the NSTrackingMouseEnteredAndExited option receives a mouseEntered: whenever the mouse cursor enters the region of a view defined by the tracking-area object; subsequently, it receives a mouseExited: messages when the mouse leaves that region. If the NSTrackingMouseMoved option is also specified for the tracking-area object, the owner also receives one or more mouseMoved: messages between each mouseEntered: and mouseExited: message. You override the corresponding NSResponder methods to handle these messages to perform such tasks as highlighting the view, displaying a custom tool tip, or displaying related information in another view.
The tracking code in Listing 6-2 is used in making an “eyeball” follow the movement of the mouse pointer when it enters a tracking rectangle.
Listing 6-3 Handling mouse-entered, mouse-moved, and mouse-exited events
- (void)mouseEntered:(NSEvent *)theEvent { |
NSPoint eyeCenter = [self convertPoint:[theEvent locationInWindow] fromView:nil]; |
eyeBox = NSMakeRect((eyeCenter.x-10.0), (eyeCenter.y-10.0), 20.0, 20.0); |
[self setNeedsDisplayInRect:eyeBox]; |
[self displayIfNeeded]; |
} |
- (void)mouseMoved:(NSEvent *)theEvent { |
NSPoint eyeCenter = [self convertPoint:[theEvent locationInWindow] fromView:nil]; |
eyeBox = NSMakeRect((eyeCenter.x-10.0), (eyeCenter.y-10.0), 20.0, 20.0); |
[self setNeedsDisplayInRect:eyeBox]; |
[self displayIfNeeded]; |
} |
- (void)mouseExited:(NSEvent *)theEvent { |
[self resetEye]; |
[self setNeedsDisplayInRect:eyeBox]; |
[self displayIfNeeded]; |
} |
Just as you can with mouse-down and mouse-up messages, you can query the passed-in NSEvent objects to get information related to the event.
Last updated: 2007-03-16