macOS开发之NSTrackingArea

2021-04-29  本文已影响0人  chasitu

之前从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

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

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];
    }
}

完结

上一篇下一篇

猜你喜欢

热点阅读