1

NSTrackingArea 定义的区域捕获鼠标事件时如何调用自己的方法?我可以在下面的 NSTrackingArea init 中指定我自己的方法(例如“myMethod”)吗?

trackingAreaTop = [[NSTrackingArea alloc] initWithRect:NSMakeRect(0,0,100,100) options:(NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveAlways) owner:myMethod userInfo:nil];

谢谢!

4

1 回答 1

1

我可以在下面的 NSTrackingArea init 中指定我自己的方法(例如“myMethod”)吗?

trackingAreaTop = [[NSTrackingArea alloc] initWithRect:NSMakeRect(0,0,100,100) options:(NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveAlways) owner:myMethod userInfo:nil];

不,owner应该是接收请求的鼠标跟踪、鼠标移动或光标更新消息的对象,而不是方法。如果您传入自定义方法,它甚至不会编译。

NSTrackingArea 定义的区域捕获鼠标事件时如何调用自己的方法?

NSTrackingArea仅定义对鼠标移动敏感的区域。为了回应他们,您需要:

  1. 使用该方法将跟踪区域添加到要跟踪的视图中addTrackingArea:
  2. 根据您的需要,可以选择在您的类中实现mouseEntered:mouseMoved:mouseExited:方法。owner

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:frame options:NSTrackingMouseEnteredAndExited owner:self userInfo:nil];
        [self addTrackingArea:trackingArea];
    }

    return self;
}    

- (void)mouseEntered:(NSEvent *)theEvent {
    NSPoint mousePoint = [self convertPoint:[theEvent locationInWindow] fromView:nil];
    // do what you desire
}

有关详细信息,请参阅使用跟踪区域对象


顺便说一句,如果您想响应鼠标单击事件而不是鼠标移动事件,则无需使用NSTrackingArea. 继续执行mouseDown:ormouseUp:方法。

于 2015-05-18T06:32:28.040 回答