Mouse Cursor on Layered NSView Cocoa

Viewed 25

I have few views, some are siblings, and some are subviews.

I need to have different Mouse Cursors. It works if I have only one view. But as soon as I move to overlapped view or a subview, the mouseExit of first and mouseEnter of second doesn't give me desired result, and I get the default Arrow cursor.

This image will help to understandenter image description here

If I start moving mouse from left to right. The cursor changes to Hand, and in the overlapped it changes to Grab, but as I end the overlapped it become Arrow.

Moving from right to left gives me opposite, starts with Grab Cursor, changes to Hand in the overlapped area, and Arrow while I expect Hand Cursor.

The codes:

@implementation HandView

- (void)updateTrackingAreas
{
    [super updateTrackingAreas];

    if (self.trackingArea)
    {
        [self removeTrackingArea:self.trackingArea];
    }

    NSTrackingAreaOptions options = NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow;
    self.trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:options owner:self userInfo:nil];
    [self addTrackingArea:self.trackingArea];
}



- (void)mouseEntered:(NSEvent *)event
{
    [super mouseEntered:event];
    [[NSCursor pointingHandCursor] set];
}

- (void)mouseExited:(NSEvent *)event
{
    [super mouseExited:event];
    [[NSCursor arrowCursor] set];
}

And

@implementation GrabView

- (void)updateTrackingAreas
{
    [super updateTrackingAreas];

    if (self.trackingArea)
    {
        [self removeTrackingArea:self.trackingArea];
    }

    NSTrackingAreaOptions options = NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow;
    self.trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:options owner:self userInfo:nil];
    [self addTrackingArea:self.trackingArea];
}



- (void)mouseEntered:(NSEvent *)event
{
    [super mouseEntered:event];
    [[NSCursor closedHandCursor] set];
}

- (void)mouseExited:(NSEvent *)event
{
    [super mouseExited:event];
    [[NSCursor arrowCursor] set];
}
@end

Any directions would help me for fix this. Thanks.

0 Answers
Related