How do I help prevent my users from initiating a drag accidentally in Cocoa?

橙三吉。 提交于 2019-12-10 12:17:09

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/1472671/how-do-i-help-prevent-my-users-from-initiating-a-drag-accidentally-in-cocoa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!