Because NSTrackingArea objects are owned by their views, the Application Kit can automatically recompute the tracking-area regions when the view is added or removed from a window or when the view changes position within its window. But in situations where the Application Kit cannot recompute an affected tracking area (or areas), it sends updateTrackingAreas to the associated view, asking it to recompute and reset the areas. One such situation is when a change in view location affects the view’s visible rectangle (visibleRect)—unless the NSTrackingArea object for that view was created with the NSTrackingInVisibleRect option, in which case the Application Kit handles the recomputation. Note that the Application Kit sends updateTrackingAreas to every view whether it has a tracking area or not.
You can override the updateTrackingAreas method as shown in Listing 6-2 to remove the current tracking areas from their views, release them, and then add new NSTrackingArea objects to the same views with recomputed regions.
Listing 6-2 Resetting a tracking-area object
- (void)updateTrackingAreas { |
NSRect eyeBox; |
[self removeTrackingArea:trackingArea]; |
[trackingArea release]; |
eyeBox = [self resetEye]; |
trackingArea = [[NSTrackingArea alloc] initWithRect:eyeBox |
options: (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow) |
owner:self userInfo:nil]; |
[self addTrackingArea:trackingArea]; |
} |
If your class is not a custom view class, you can register your class instance as an observer for the notificationNSViewFrameDidChangeNotification and have it reestablish the tracking rectangles on receiving the notification.
Last updated: 2007-03-16