我有一个带有图像的面板,我想让它可以通过“拖动”将文件(不是图像,图像仅作为文件的图标)复制到文件夹中应用程序外部的图像以及接受拖入其中的文件的任何其他应用程序(例如 Finder)。我怎样才能做到这一点?
我实施了NSDraggingSource https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Protocols/NSDraggingSource_Protocol/Reference/Reference.html协议,但我不知道如何使图像可拖动。它目前位于一个ImageView
,它位于一个ImageViewCell
.
这是我实现的协议:
#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
我添加了方法- (id)initWithCoder(NSCode *)coder
我还添加了
- (BOOL)acceptesFirstMouse:(NSEvent *)event { return YES; }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)