views:

93

answers:

1

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?

+2  A: 

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.

Peter Hosey
Your solution of recording the locationInWindow in mouseDown and comparing it to the locationInWindow on mouseDragged worked. I didn't see where HIMouseTrackingGetParameters and kMouseParamsDragInitiation came into play though. I just used a c distance function to compute the distance and compare it.
Austin
That function gives you the correct minimum distance to initiate a drag. If the user hasn't moved that distance, they're just holding down the mouse button.
Peter Hosey