macOS - mouseExited: isn't called when mouse leaves trackingArea while scrolling

在给NSView 添加trackingArea后,鼠标不动,滚动view, mouseExited: 不调用

- (void)updateTrackingAreas {
    
    [self removeTrackingArea:_trackingArea];
    _trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingActiveInKeyWindow |NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited owner:self userInfo:nil];
    [self addTrackingArea:_trackingArea];
}

解决方法:

- (void)updateTrackingAreas {
    
    [self removeTrackingArea:_trackingArea];
    _trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingActiveInKeyWindow |NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited owner:self userInfo:nil];
    [self addTrackingArea:_trackingArea];
    
    //mouseExited isn't called when mouse leaves trackingArea while scrolling
    NSPoint mouseLocation = [[self window] mouseLocationOutsideOfEventStream];
    mouseLocation = [self convertPoint: mouseLocation
                              fromView: nil];
    
    if (NSPointInRect(mouseLocation, [self bounds]))
    {
        [self mouseEntered: [NSApp currentEvent]];
    }
    else
    {
        [self mouseExited: [NSApp currentEvent]];
    }
    
    [super updateTrackingAreas];
    
}
发布了34 篇原创文章 · 获赞 20 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/ZhangWangYang/article/details/93504745