Distinguishing a single click from a double click in Cocoa on the Mac

后端 未结 5 1008
一向
一向 2021-02-01 21:05

I have a custom NSView (it\'s one of many and they all live inside an NSCollectionView — I don\'t think that\'s relevant, but who knows). When I click

5条回答
  •  孤街浪徒
    2021-02-01 21:30

    Delaying the changing of the selection state is (from what I've seen) the recommended way of doing this.

    It's pretty simple to implement:

    - (void)mouseUp:(NSEvent *)theEvent
    {
        if([theEvent clickCount] == 1) {
            [model performSelector:@selector(toggleSelectedState) afterDelay:[NSEvent doubleClickInterval]];
        }
        else if([theEvent clickCount] == 2)
        {
            if([model hasBeenDownloaded])
            {
                    [NSRunLoop cancelPreviousPerformRequestsWithTarget: model]; 
                    [mainWindowController showPreviewWindowForPicture:model];
            }
        }
    }
    

    (Notice that in 10.6, the double click interval is accessible as a class method on NSEvent)

提交回复
热议问题