Why are Tracking Areas Not Respected

Go To StackoverFlow.com

1

I've been experimenting with tracking area, and having some problems, so I created this simple program as a test. I create one tracking area in the lower left corner of my view (which is the window's content view), but I receive mouseEntered and exited messages no matter where I enter or exit the view. I've also tried putting this code in the init method and awakeFromNib with the same results.

@implementation Parent //This view is the contentView of the main window

-(void)viewDidMoveToWindow{
    NSLog(@"In viwDidMoveToWindow");
    NSTrackingArea *area = [[NSTrackingArea alloc]initWithRect:NSMakeRect(0,0,50,50) options:NSTrackingInVisibleRect |NSTrackingMouseEnteredAndExited |NSTrackingActiveInActiveApp owner:self userInfo:nil];
    [self addTrackingArea:area];
}

-(void)mouseEntered:(NSEvent *)theEvent {
    NSLog(@"Entered");
}

-(void)mouseExited:(NSEvent *)theEvent {
    NSLog(@"Exited");
}

@end

Why is the tracking area not being respected?

2012-04-04 02:23
by rdelmar


1

It has to do with the options you are using, try instead using

options:NSTrackingActiveAlways | NSTrackingMouseEnteredAndExited
2012-04-04 04:02
by Grant Wilkinson
So why does NSTrackingInVisibleRect not work as I expect? I thought the purpose of that choice was to only allow tracking in the visible portion of the rect specified in the tracking area -- it seems to be in the whole view that the tracking rectangle is added to - rdelmar 2012-04-04 04:16
Ok, I get it now -- I reread the docs, and it says with the NSTrackingInVisibleRect option, the value returned by rect is ignored, and the rect is automatically updated with changes in the visible rect - rdelmar 2012-04-04 04:32
It would seem that you could've used NSTrackingInVisibleRect in this case but according to the docs the value returned from rect is ignored. I found this answer from searching to be quite similar linkGrant Wilkinson 2012-04-04 04:45