macOS-鼠标

鼠标跟踪

如果直接重写
-(void)mouseMoved:(NSEvent *)event
-(void)mouseExited:(NSEvent *)event
-(void)mouseEntered:(NSEvent *)event
鼠标经过时是不会触发的,需要给NSView添加一个NSTrackingArea

-(void)awakeFromNib
{
    [self addTrackingArea];
}
-(void)mouseMoved:(NSEvent *)event
{
    NSLog(@"-- mouseMoved ");
}
-(void)mouseExited:(NSEvent *)event
{
    NSLog(@"-- mouseExited ");
}
-(void)mouseEntered:(NSEvent *)event
{
    NSLog(@"-- mouseEntered ");
}

-(void)addTrackingArea
{
    NSTrackingArea *are = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow owner:self userInfo:nil];
    [self addTrackingArea:are];
}
  • TrackingActive 参数必须加上 , 否则报错:
    Failed to set (contentViewController) user defined inspected property on (NSWindow): trackingArea options 0x3 do not specify when the tracking area is active
    本例中参数添加的是NSTrackingActiveInKeyWindow

鼠标样式

鼠标经过view部分区域是改变样式:
macOS-鼠标_第1张图片

当鼠标经过TestSubView的(0,0,40,40)区域是会改变状态

你可能感兴趣的:(macOS-鼠标)