在Mac上区分Cocoa中的单击和双击 [英] Distinguishing a single click from a double click in Cocoa on the Mac

查看:230
本文介绍了在Mac上区分Cocoa中的单击和双击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义 NSView (它是许多人之一,他们都住在一个 NSCollectionView 认为这是相关的,但谁知道)。当我点击视图,我想让它改变其选择状态(并重新绘制自己相应);当我双击视图时,我想让它为刚刚双击的对象弹出一个更大的预览窗口。

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 the view, I want it to change its selection state (and redraw itself accordingly); when I double-click the view, I want it to pop up a larger preview window for the object that was just double-clicked.

我的第一个看起来像这样: / p>

My first looked like this:

- (void)mouseUp: (NSEvent *)theEvent {
    if ([theEvent clickCount] == 1) [model setIsSelected: ![model isSelected]];
    else if ([theEvent clickCount] == 2) if ([model hasBeenDownloaded]) [mainWindowController showPreviewWindowForPicture:model];
}

除非,当我双击视图时,选择状态改变,窗口弹出。这不是我想要的。

which mostly worked fine. Except, when I double-click the view, the selection state changes and the window pops up. This is not exactly what I want.

似乎我有两个选择。我可以恢复选择状态响应双击(撤消错误的单击)或者我可以finagle某种 NSTimer 解决方案构建在延迟之前响应单击。换句话说,我可以确保在更改选择状态之前不会第二次点击。

It seems like I have two options. I can either revert the selection state when responding to a double-click (undoing the errant single-click) or I can finagle some sort of NSTimer solution to build in a delay before responding to the single click. In other words, I can make sure that a second click is not forthcoming before changing the selection state.

这看起来更加优雅,所以这是我第一次采取的方法。我从Google发现的唯一真正的指导是在一个未命名的网站,其名称有连字符。

This seemed more elegant, so it was the approach I took at first. The only real guidance I found from Google was on an unnamed site with a hyphen in its name. This approach mostly works with one big caveat.

未解决的问题是我的 NSTimer 等待多长时间? 。未命名的站点建议使用Carbon函数 GetDblTime()。除了在64位应用程序不可用,我可以找到它唯一的文档说,它返回时钟滴答。我不知道如何将这些转换为秒 NSTimer

The outstanding question is "How long should my NSTimer wait?". The unnamed site suggests using the Carbon function GetDblTime(). Aside from being unusable in 64-bit apps, the only documentation I can find for it says that it's returning clock-ticks. And I don't know how to convert those into seconds for NSTimer.

那么这里的正确答案是什么?使用 GetDblTime()? 撤消双击的选择?我不知道可可惯用的方法。

So what's the "correct" answer here? Fumble around with GetDblTime()? "Undo" the selection on a double-click? I can't figure out the Cocoa-idiomatic approach.

推荐答案

延迟选择状态的改变是(从什么I'

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

实现起来非常简单:

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

双击间隔可以作为 NSEvent )上的类方法访问

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

这篇关于在Mac上区分Cocoa中的单击和双击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆