NSTrackingArea works weird - Entire View, or nothing… no rectangles respected

后端 未结 2 787
野的像风
野的像风 2021-01-15 22:05

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:

  1. Withou
相关标签:
2条回答
  • 2021-01-15 22:16

    Straight from the docs for NSTrackingInVisibleRect:

    The NSTrackingArea object is automatically synchronized with changes in the view’s visible area (visibleRect) and the value returned from rect is ignored.

    0 讨论(0)
  • 2021-01-15 22:38

    Mike Abdullah's answer explains point 2.

    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's documentation says:

    When creating a tracking-area object, you specify a rectangle (in the view’s coordinate system), ...

    0 讨论(0)
提交回复
热议问题