2013-12-10 10 views
10

在Mac OS X上,對單詞進行三指敲擊會彈出一個窗口,並顯示該單詞的定義。如何覆蓋NSTextView中的三指龍頭行爲?

Image showing a pop up window showing a word definition.

這個成語也是在Xcode中,用在做了一個符號3指輕按顯示其文檔,就像如果它一直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 

回答

7

起反應在一個NSTextView三重輕拍可以很容易地通過重寫quickLookWithEvent:來完成。

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

它也教會了我,你可以三次點擊任何東西來調用Quick Look。

+0

@zneak遲了幾個月,但'quickLookWithEvent:'被記錄爲[在10.8中的'NSResponder'中](https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes /NSResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/NSResponder/quickLookWithEvent :) – andlabs

+0

哦,甜美。我想我只是錯過了。 – zneak

2

子類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 
    } 
} 

以後編輯:

我發現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]; 
} 
+0

您的'mouseDown:'示例適用於連續3次點擊,但不能捕獲用3個手指做出的點擊。 – zneak

+0

@zneak我意識到速度非常快,所以我添加了第二部分。我真的認爲這可以導致解決您的問題,如果它可用於觸控板,同時我發現它不是。但是,我在我的答案中編輯的蘋果文檔中找到了一些東西。 – nestedloop