如何帮助阻止我的用户在Cocoa中意外启动拖动? [英] How do I help prevent my users from initiating a drag accidentally in Cocoa?
问题描述
我有一个集合视图,我已经子类化,允许我通过拖放重新排序集合视图项。我的拖拽代码设置的Pasterboard目前在mouseDragged:
- (void)mouseDragged:(NSEvent *)aEvent {
if(!dragInProgress){
dragInProgress = YES;
NSPasteboard * pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
... setup pboard,declare types,setData ...
...创建拖动图像....
[self dragImage:image
at:position
offset:NSZeroSize
event:aEvent
pasteboard:pboard
source:self
slideBack:YES];
}
}
我想只在用户已经拖动了一定长度,所以他们不会意外启动拖拽。是否有一个设置在Cocoa中执行此操作,或者需要将此代码移动到mouseMoved:并检查拖动开始位置和鼠标当前位置之间的距离。
mouseDown:
)。在 mouseDragged:
中,从鼠标拖动事件的位置中减去鼠标按下事件的位置,并将该差异与 HIMouseTrackingGetParameters
与 kMouseParamsDragInitiation
选择器。 I have a collection view that I've subclassed that allows me to reorder the collection view items via drag and drop. My drag code that sets up the pasterboard is currently in mouseDragged:
- (void)mouseDragged:(NSEvent *)aEvent {
if(!dragInProgress) {
dragInProgress = YES;
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
... setup pboard, declare types, setData ...
... create drag image ....
[self dragImage: image
at: position
offset: NSZeroSize
event: aEvent
pasteboard: pboard
source: self
slideBack: YES];
}
}
I would like to only initiate a drag if the user has dragged for a certain length, so they don't initiate a drag accidentally. Is there a setting to do this in Cocoa, or do I need to move this code to mouseMoved: and check the distance between where the drag started and where the mouse is currently?
In mouseDown:
, remember where the mouse went down (locationInWindow
). In mouseDragged:
, subtract the location of the mouse-down event from the location of the mouse-dragged event, and compare the difference to the size returned by HIMouseTrackingGetParameters
with the kMouseParamsDragInitiation
selector.
这篇关于如何帮助阻止我的用户在Cocoa中意外启动拖动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!