NSResponder似乎没有鼠标双击事件。有没有简单的方法来捕获双击?
NSResponder似乎缺少鼠标双击事件。是否存在一种简单的方式来捕捉双击事件?NSResponder似乎没有鼠标双击事件。有没有简单的方法来捕获双击?
NSResponder似乎缺少鼠标双击事件。是否存在一种简单的方式来捕捉双击事件?mouseDown:
和mouseUp:
方法需要一个NSEvent对象作为参数,该对象包含有关点击的信息,包括clickCount
(点击次数)。
普通的clickCount
解决方案存在的问题是双击被视为两次单击。我的意思是,您仍然会得到单击。如果您想对该单击有不同的反应,则需要在纯粹的计数基础上添加一些东西。以下是我最终采用的(使用Swift):
private var _doubleClickTimer: NSTimer?
// a way to ignore first click when listening for double click
override func mouseDown(theEvent: NSEvent) {
if theEvent.clickCount > 1 {
_doubleClickTimer!.invalidate()
onDoubleClick(theEvent)
} else if theEvent.clickCount == 1 { // can be 0 - if delay was big between down and up
_doubleClickTimer = NSTimer.scheduledTimerWithTimeInterval(
0.3, // NSEvent.doubleClickInterval() - too long
target: self,
selector: "onDoubleClickTimeout:",
userInfo: theEvent,
repeats: false
)
}
}
func onDoubleClickTimeout(timer: NSTimer) {
onClick(timer.userInfo as! NSEvent)
}
func onClick(theEvent: NSEvent) {
println("single")
}
func onDoubleClick(theEvent: NSEvent) {
println("double")
}
mouseDown:
和mouseUp:
生成的NSEvent
具有一个名为clickCount
的属性。检查它是否为2,以确定是否发生了双击。
示例实现:
- (void)mouseDown:(NSEvent *)event {
if (event.clickCount == 2) {
NSLog(@"Double click!");
}
}
只需将此放置在您的NSResponder
(例如NSView
)子类中即可。
我更喜欢的一种替代 mouseDown:
+ NSTimer
方法是使用 NSClickGestureRecognizer
。
let doubleClickGestureRecognizer = NSClickGestureRecognizer(target: self, action: #selector(self.myCustomMethod))
doubleClickGestureRecognizer.numberOfClicksRequired = 2
self.myView.addGestureRecognizer(doubleClickGestureRecognizer)
我实现了类似@jayarjo的东西,但这个更加模块化,你可以将其用于任何NSView或其子类。这是一个自定义手势识别器,它将识别单击和双击操作,但在双击阈值过去之前不会识别单击:
//
// DoubleClickGestureRecognizer.swift
//
import Foundation
/// gesture recognizer to detect two clicks and one click without having a massive delay or having to implement all this annoying `requireFailureOf` boilerplate code
final class DoubleClickGestureRecognizer: NSClickGestureRecognizer {
private let _action: Selector
private let _doubleAction: Selector
private var _clickCount: Int = 0
override var action: Selector? {
get {
return nil /// prevent base class from performing any actions
} set {
if newValue != nil { // if they are trying to assign an actual action
fatalError("Only use init(target:action:doubleAction) for assigning actions")
}
}
}
required init(target: AnyObject, action: Selector, doubleAction: Selector) {
_action = action
_doubleAction = doubleAction
super.init(target: target, action: nil)
}
required init?(coder: NSCoder) {
fatalError("init(target:action:doubleAction) is only support atm")
}
override func mouseDown(with event: NSEvent) {
super.mouseDown(with: event)
_clickCount += 1
let delayThreshold = 0.15 // fine tune this as needed
perform(#selector(_resetAndPerformActionIfNecessary), with: nil, afterDelay: delayThreshold)
if _clickCount == 2 {
_ = target?.perform(_doubleAction)
}
}
@objc private func _resetAndPerformActionIfNecessary() {
if _clickCount == 1 {
_ = target?.perform(_action)
}
_clickCount = 0
}
}
使用方法:
let gesture = DoubleClickGestureRecognizer(target: self, action: #selector(mySingleAction), doubleAction: #selector(myDoubleAction))
button.addGestureRecognizer(gesture)
@objc func mySingleAction() {
// ... single click handling code here
}
@objc func myDoubleAction() {
// ... double click handling code here
}
就我个人而言,我会检查双击鼠标松开函数:
- (void)mouseUp:(NSEvent *)theEvent
{
if ([theEvent clickCount] == 2)
{
CGPoint point = [theEvent locationInWindow];
NSLog(@"Double click on: %f, %f", point.x, point.y);
}
}