macOS开发之NSTrackingArea

之前从iOS开发转战这里的人小伙伴对NSTrackingArea类应该会有点陌生,毕竟手机开发中不需要监听鼠标的移动,这个类就是跟我们mac常用鼠标有关,我们简单了解一下我们常用的一些使用场景

mouseEntered和mouseExited

NSView要想响应鼠标进出事件,需要重写NSView的updateTrackingAreas方法,updateTrackingAreas 是View的大小发生改变的时候进行的回调,让我们在这个时机有机会重新去设置View的追踪区域NSTrackingArea,在设置NSTrackingArea的时候,可以决定要响应鼠标的哪些事件,看下面例子:



红色区域的是SuperView,蓝色区域的是SubView

#import "SuperView.h"

@implementation SuperView

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}

- (void)updateTrackingAreas {
    NSArray * trackingAreas = [self trackingAreas];
    for (NSTrackingArea *area in trackingAreas) {
        [self removeTrackingArea:area];
    }
    
    NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds
                                                                options:(NSTrackingMouseEnteredAndExited |
                                                                         NSTrackingMouseMoved |
                                                                         NSTrackingActiveInActiveApp |
                                                                         NSTrackingInVisibleRect |
                                                                         NSTrackingAssumeInside |
                                                                         NSTrackingCursorUpdate)
                                                                  owner:self
                                                               userInfo:nil];
    [self addTrackingArea:trackingArea];
}

- (void)mouseEntered:(NSEvent *)event {
    NSLog(@"MacLean SuperView mouseEntered");
}

- (void)mouseExited:(NSEvent *)event {
    NSLog(@"MacLean SuperView mouseExited");
}

@end
  • 代码中有个options的值有好多,除了移动和进出两个枚举以外,其它的这四位一定要加上,要不然方法不调用,具体的原因不多解释了应该能看懂枚举的意思哈
    NSTrackingActiveInActiveApp |NSTrackingInVisibleRect |NSTrackingAssumeInside |NSTrackingCursorUpdate
  • 我们发现在子View中调用super,会让事件在响应链上传播,会让父View也响应到事件,也就是说,如果子View明确处理事件,就不应该调用super,在NSView(Handling Events in Your Subclass)的文档中对此进行了说明。

mouseDown和mouseUp

mouseDown和mouseUp事件是不需要在updateTrackingAreas中设置的,默认都能响应的,如果不想父View响应,那么只要不调用super,不让事件在响应链上传播就可以了。

- (void)mouseDown:(NSEvent *)event {
//    [super mouseDown:event];
    NSLog(@"MacLean SubView mouseDown");
}

- (void)mouseUp:(NSEvent *)event {
//    [super mouseUp:event];
    NSLog(@"MacLean SubView mouseUp");
}

//SuperView
- (void)mouseDown:(NSEvent *)event {
    NSLog(@"MacLean SuperView mouseDown");
}

- (void)mouseUp:(NSEvent *)event {
    NSLog(@"MacLean SuperView mouseUp");
}

SubView中调用了super,结果

2020-06-19 10:49:28.585185+0800 MacLearn[21625:3218490] MacLean SuperView mouseDown
2020-06-19 10:49:28.585257+0800 MacLearn[21625:3218490] MacLean SubView mouseDown
2020-06-19 10:49:28.708589+0800 MacLearn[21625:3218490] MacLean SuperView mouseUp
2020-06-19 10:49:28.708654+0800 MacLearn[21625:3218490] MacLean SubView mouseUp
2020-06-19 10:49:31.387772+0800 MacLearn[21625:3218490] MacLean SuperView mouseDown
2020-06-19 10:49:31.533055+0800 MacLearn[21625:3218490] MacLean SuperView mouseUp
  • NSView的事件回调方法默认都是调用super,也就是说在子View上鼠标事件会通过响应链传递到父View的,如果,只想子View处理鼠标事件,需要重写NSView的事件回调方法,去掉super的调用。

mouseMoved

NSView要想响应mouseMoved事件是需要在updateTrackingAreas中设置的,我们看看下面的例子,鼠标从下滑到上

\\ SubView
- (void)mouseMoved:(NSEvent *)event {
    NSLog(@"MacLean SubView mouseMoved");
}

\\SuperView
- (void)mouseMoved:(NSEvent *)event {
    NSLog(@"MacLean SuperView mouseMoved");
}
2020-06-19 10:57:00.707662+0800 MacLearn[21716:3223044] MacLean SuperView mouseEntered
2020-06-19 10:57:00.808957+0800 MacLearn[21716:3223044] MacLean SuperView mouseMoved
2020-06-19 10:57:00.876397+0800 MacLearn[21716:3223044] MacLean SuperView mouseMoved
2020-06-19 10:57:00.887334+0800 MacLearn[21716:3223044] MacLean SuperView mouseMoved
2020-06-19 10:57:02.169826+0800 MacLearn[21716:3223044] MacLean SuperView mouseMoved
2020-06-19 10:57:02.181265+0800 MacLearn[21716:3223044] MacLean SuperView mouseMoved
2020-06-19 10:57:02.192857+0800 MacLearn[21716:3223044] MacLean SuperView mouseMoved
2020-06-19 10:57:02.203424+0800 MacLearn[21716:3223044] MacLean SubView mouseEntered
2020-06-19 10:57:02.203884+0800 MacLearn[21716:3223044] MacLean SuperView mouseMoved
2020-06-19 10:57:02.203943+0800 MacLearn[21716:3223044] MacLean SubView mouseMoved
2020-06-19 10:57:02.214461+0800 MacLearn[21716:3223044] MacLean SuperView mouseMoved
2020-06-19 10:57:02.214565+0800 MacLearn[21716:3223044] MacLean SubView mouseMoved
2020-06-19 10:57:06.309816+0800 MacLearn[21716:3223044] MacLean SuperView mouseMoved
2020-06-19 10:57:06.309949+0800 MacLearn[21716:3223044] MacLean SubView mouseMoved
2020-06-19 10:57:06.366530+0800 MacLearn[21716:3223044] MacLean SuperView mouseMoved
2020-06-19 10:57:06.366640+0800 MacLearn[21716:3223044] MacLean SubView mouseMoved
2020-06-19 10:57:08.773979+0800 MacLearn[21716:3223044] MacLean SuperView mouseMoved
2020-06-19 10:57:08.774084+0800 MacLearn[21716:3223044] MacLean SubView mouseMoved
2020-06-19 10:57:08.785382+0800 MacLearn[21716:3223044] MacLean SubView mouseExited
2020-06-19 10:57:08.786015+0800 MacLearn[21716:3223044] MacLean SuperView mouseMoved
2020-06-19 10:57:08.796243+0800 MacLearn[21716:3223044] MacLean SuperView mouseMoved
2020-06-19 10:57:08.807529+0800 MacLearn[21716:3223044] MacLean SuperView mouseMoved
2020-06-19 10:57:08.818352+0800 MacLearn[21716:3223044] MacLean SuperView mouseMoved
2020-06-19 10:57:08.829537+0800 MacLearn[21716:3223044] MacLean SuperView mouseMoved
2020-06-19 10:57:09.347431+0800 MacLearn[21716:3223044] MacLean SuperView mouseExited

发现没,我们在SubView中没有调用super,结果,在SubView中滑动的时候,SuperView居然也能响应move事件,具体原因不详。我们看看SubView中调用super会是什么表现呢?

2020-06-19 11:12:20.857676+0800 MacLearn[21842:3229852] MacLean SuperView mouseEntered
2020-06-19 11:12:20.858661+0800 MacLearn[21842:3229852] MacLean SuperView mouseMoved
2020-06-19 11:12:20.867886+0800 MacLearn[21842:3229852] MacLean SuperView mouseMoved
2020-06-19 11:12:20.884067+0800 MacLearn[21842:3229852] MacLean SuperView mouseMoved
2020-06-19 11:12:20.890665+0800 MacLearn[21842:3229852] MacLean SuperView mouseMoved
2020-06-19 11:12:21.070717+0800 MacLearn[21842:3229852] MacLean SuperView mouseMoved
2020-06-19 11:12:21.083426+0800 MacLearn[21842:3229852] MacLean SuperView mouseMoved
2020-06-19 11:12:21.093463+0800 MacLearn[21842:3229852] MacLean SubView mouseEntered
2020-06-19 11:12:21.094096+0800 MacLearn[21842:3229852] MacLean SuperView mouseMoved
2020-06-19 11:12:21.094205+0800 MacLearn[21842:3229852] MacLean SuperView mouseMoved
2020-06-19 11:12:21.094260+0800 MacLearn[21842:3229852] MacLean SubView mouseMoved
2020-06-19 11:12:21.104596+0800 MacLearn[21842:3229852] MacLean SuperView mouseMoved
2020-06-19 11:12:21.104705+0800 MacLearn[21842:3229852] MacLean SuperView mouseMoved
2020-06-19 11:12:21.104748+0800 MacLearn[21842:3229852] MacLean SubView mouseMoved
2020-06-19 11:12:21.115910+0800 MacLearn[21842:3229852] MacLean SuperView mouseMoved
2020-06-19 11:12:21.116023+0800 MacLearn[21842:3229852] MacLean SuperView mouseMoved

move的表现还是很特殊的,在子View中移动,不管子View中有没有调用super,父View的move事件都会响应。

Tool Tip
toop tip是NSView提供的一个功能,运行hoverNSView的时候文案提示。
调用方法设置就可以了,很简单。可是在实际开发中遇到了问题,居然不起作用,最后发现是和NSTrackingArea有关系。NSView可能会有多个NSTackingArea,我们在设置鼠标事件的时候,不能无脑的把NSView的所有NSTackingArea都remove掉,只需要把我们设置的NSTackingArea remove掉就可以了

- (void)updateTrackingAreas
{
    if (hovered) {
        hovered = NO;
        if (self.delegate && [self.delegate respondsToSelector:@selector(baseView:mouseExited:)]) {
            [self.delegate baseView:self mouseExited:nil];
        }
    }
    
    if (_trackingArea) {
        [self removeTrackingArea:_trackingArea];
    }
    
    if (self.userInteraction && self.needTracking && !self.isHidden) {
        _trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:(NSTrackingMouseEnteredAndExited |NSTrackingMouseMoved|NSTrackingActiveInActiveApp |NSTrackingInVisibleRect |NSTrackingAssumeInside |NSTrackingCursorUpdate) owner:self userInfo:nil];
        [self addTrackingArea:_trackingArea];
    }
}
  • macOS开发相对比较小众开发人员不多,导致官方维护的也不好,使用起来各种难受,遇到问题资料也少。
    电脑和手机开发有些不一样,最大的不同就是对事件的处理,在手机上是用手指操作的,只有点击动作(长按、双击),在电脑上是用鼠标操作的,除了点击动作还有鼠标hover的动作,和电视上有个selected状态类似,mouseEntered和mouseExited就是hover的两个回调。
    需要说明的是,macOS的hover事件和click事件的传递和Android中事件传递是不一样的,我们看看在子view中点击断点看看:

    看到没,是Application kit直接把事件分发到了子view上,没有经过父view,这个和Android是不一样的。
    还有比较奇葩的是,在子View的mouseDown中把子View从父View上移除掉时,子View的mouseUp居然不会被回调了,而且会回调到父View的mouseUp上,如果父View的mouseUp中做了一些工作就会出错。mouseDown和mouseUp成对出现,能理解,毕竟实际动作就是这样的,可是系统居然不判断接收目标对不对,我觉得这应该是macOS的bug。
    解决办法时,在mouseUp响应的时候,(转)看一下自身的mouseDown有没有响应过。

完结

你可能感兴趣的:(macOS开发之NSTrackingArea)