mouseUp not firing in Cocoa?

自古美人都是妖i 提交于 2021-02-10 05:52:24

问题


I'm trying to implement drag&drop functionality in my app and I ran into a problem of my mouseUp event not firing. It fires just fine if I simply click and release on my view, but if I do drag, my mouseDragged event fires and that's it.

By trying different things I figured out that the problem is in my dragImage call, but I don't know how to solve it for now.

Here is my code:

-(void) mouseDragged:(NSEvent *)theEvent
{
   isDragging = YES;

   if ([selectedCellRowIndex longValue] >= 0)
   {
      NSImage *im = [[NSImage alloc]initWithContentsOfFile:@"/Users/UserName/Desktop/drag.png"];
      NSSize dragOffset = NSMakeSize(0.0, 0.0);
      NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];

      [pboard declareTypes:[NSArray arrayWithObject:NSTIFFPboardType]  owner:self];
      [pboard setData:[im TIFFRepresentation] forType:NSTIFFPboardType];

      [self dragImage:im
                   at:downPointRelativeToTable
               offset:dragOffset
                event:theEvent
           pasteboard:pboard
               source:self
            slideBack:YES];
  }
} 

Does anyone know where my problem can be?! Thank you!


回答1:


In your code snippet you're getting the NSPasteboard instance but not copying your data to it. That's required, otherwise your drag won't be initiated properly - that's probably why you'll never get the mouseUp: event.

The docs for dragImage:at:offset:event:pasteboard:source:slideBack: state that

Before invoking this method, you must place the data to be transferred on pboard. To do this, get the drag pasteboard object (NSDragPboard), declare the types of the data, and then put the data on the pasteboard.




回答2:


I saw this with a simple NSView descendant. On mouseDown a drag operation was initiated and mouseUp never was triggered. Instead the call to super blocked until the drag operation was done (a local run loop obviously). Consequently I had to put my mouseUp handling in the mouseDown function after the call returned from super.



来源:https://stackoverflow.com/questions/15824091/mouseup-not-firing-in-cocoa

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