Cocoa OSX:如何使图像可拖动

时间:2021-12-02 00:22:53

I have a panel with an image on it, and I want to make it so that you can copy a file (not the image,the image is only going to server as an icon for the file) into a folder by “dragging” the image outside of the application and into any other application that accepts files being dragged into it (ex. Finder). How can I do this?

我有一个带有图像的面板,我想制作它,这样你就可以通过“拖动”将文件(不是图像,图像只作为文件的图标转到服务器)复制到文件夹中应用程序外部的图像以及接受拖入文件的任何其他应用程序(例如Finder)。我怎样才能做到这一点?

I implemented the NSDraggingSource protocol, but I’m not sure how to make the image draggable. It is currently inside of an ImageView, which is inside of an ImageViewCell.

我实现了NSDraggingSource协议,但我不确定如何使图像可拖动。它目前位于ImageView内部,位于ImageViewCell内部。

Here is the protocol I implemented:

这是我实施的协议:

#import "DragNDropView.h"

@implementation 
-(NSDragOperation)draggingSession:(NSDraggingSession *)session
sourceOperationMaskForDraggingContext: (NSDraggingContext) context{
  switch(context){
    case NSDraggingContextOutsideApplication:
      return NSDragOperationCopy;
      break;
    default:
      return NSDragOperationNone;
      break;
  }
}

-(void) draggingSession:(NSDraggingSession *)session willBeginAtPoint:(NSPoint) screenPoint{
   NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
   NSPaseBoardItem *contents = [[NSPasteboardItem alloc]
                                inithWithPasteboardPreopertyList:SDKFileName ofType:NSFileContentsPboardType];
   [pboard writeObjects[NSArray arrayWithObjects:contents, nil]];
}

-(void)drawRect:(NSRect)dirtyRect{
  SDKFileName = @"example.example";
  [super drawRect:dirtyRect];
}

@end

1 个解决方案

#1


1  

I added the method - (id)initWithCoder(NSCode *)coder and I also added
- (BOOL)acceptesFirstMouse:(NSEvent *)event { return YES; }

我添加了方法 - (id)initWithCoder(NSCode *)编码器,我还添加了 - (BOOL)acceptesFirstMouse:(NSEvent *)event {return YES; }

#1


1  

I added the method - (id)initWithCoder(NSCode *)coder and I also added
- (BOOL)acceptesFirstMouse:(NSEvent *)event { return YES; }

我添加了方法 - (id)initWithCoder(NSCode *)编码器,我还添加了 - (BOOL)acceptesFirstMouse:(NSEvent *)event {return YES; }