可可OSX:如何使图像可拖动 [英] Cocoa OSX: How can I make an image draggable

查看:60
本文介绍了可可OSX:如何使图像可拖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上面有图像的面板,我想制作一个面板,以便您可以将文件(不是图像,该图像仅作为文件的图标发送到服务器)复制到文件夹中,方法是将图像拖动"到应用程序之外,并接受任何其他接受文件拖动到其中的应用程序(例如Finder).我该怎么办?

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?

我实现了NSDraggingSource 协议,但是我不确定如何使图像可拖动.它当前在 ImageView 的内部,而在 ImageViewCell 的内部.

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.

这是我实施的协议:

#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 {返回YES;}

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

这篇关于可可OSX:如何使图像可拖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆