如何在NSTextView中覆盖3指点击行为?

时间:2013-12-10 23:20:53

标签: macos cocoa-touch

在Mac OS X上,对一个单词进行三指点击会弹出一个带有单词定义的窗口。

Image showing a pop up window showing a word definition.

这个习惯用法也用在Xcode中,在符号上用三指点击显示其文档,就像它已被alt +点击一样。

Image showing a pop up window showing symbol documentation.

当我的应用程序的用户对NSTextView中的某些令牌进行3指点击时,我想做类似的事情并显示定义。但是,我找不到如何检测用3个手指完成敲击。有人可以帮我吗?

编辑如果这会向任何人提醒任何内容,当您执行此类操作时会触发三个事件(由覆盖[NSApplication sendEvent:]捕获):

NSEvent: type=SysDefined loc=(270.918,250.488) time=417954.6 flags=0x100 win=0x0 winNum=28293 ctxt=0x0 subtype=6 data1=1818981744 data2=1818981744
NSEvent: type=SysDefined loc=(270.918,250.488) time=417954.6 flags=0x100 win=0x0 winNum=28293 ctxt=0x0 subtype=9 data1=1818981744 data2=1818981744
NSEvent: type=Kitdefined loc=(0,263) time=417954.8 flags=0x100 win=0x0 winNum=28306 ctxt=0x0 subtype=4 data1=1135411200 data2=1132691456

2 个答案:

答案 0 :(得分:8)

通过覆盖quickLookWithEvent:,可以最轻松地对NSTextView中的三次点击做出反应。

-(void)quickLookWithEvent:(NSEvent *)event
{
    NSLog(@"Look at me! %@", event);
}

它还告诉我,你可以三次点击任何东西来调用它上面的快速查看。

答案 1 :(得分:1)

子类NSTextView并覆盖鼠标按下事件(这是视图通常处理点击/点击事件的地方):

-(void)mouseDown:(NSEvent*)event
{
  if (event.clickCount == 3)
  {
    //Do your thing
  }
}

希望这有帮助。

如果三次点击对您不起作用(我目前不在Mac上检查),您可以尝试其他方法。 我知道它适用于iOS,我不知道触控板手势。

您可以尝试在视图中添加UITapGestureRecognizer:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
tapGesture.numberOfTouchesRequired = 3;

//....

-(void)viewTapped:(UITapGestureRecognizer*)tap
{
  if(tap.state == UIGestureRecognizerStateRecognized)
  {
    //you detected a three finger tap, do work
  } 
}

稍后编辑:

我在Apple文档中找到了this article。根据本文中的示例,以下是一些应该有用的代码(从清单8-5开始):

- (void)touchesBeganWithEvent:(NSEvent *)event {
    if (!self.isEnabled) return;

    NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseTouching inView:self.view];

    if (touches.count == 3) 
    {
       //Three finger touch detected
    }

    [super touchesBeganWithEvent:event];
}
相关问题