Mac os - 添加文件拖动 单文件、多文件支持

Mac os文件拖动支持,是在NSView层级别进行监听。

所以第一步写Window下的View实现。

直接贴代码


@protocol ReadFileViewDelegate <NSObject>
///接收单个文件
- (void)receivedFileUrl:(NSURL *)fileUrl;
///接收到多个文件
- (void)receivedFileUrlList:(NSArray< NSURL *> *)fileUrls;
@end

@interface ReadFileView : NSView
@property (weak, nonatomic) IBOutlet id<ReadFileViewDelegate> delegate;
@end
@implementation ReadFileView
//MARK: - life cycle
- (id)initWithFrame:(NSRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //注册文件拖动事件
        [self registerForDraggedTypes:[NSArray arrayWithObjects:NSPasteboardTypeFileURL, nil]];
    }
    
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    //注册文件拖动事件
    [self registerForDraggedTypes:[NSArray arrayWithObjects:NSPasteboardTypeFileURL, nil]];
}

- (void)dealloc {   
    [self unregisterDraggedTypes];
}

//MARK: - private methods
//当文件被拖动到界面触发
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
    NSPasteboard *pboard;
    NSDragOperation sourceDragMask;
    
    sourceDragMask = [sender draggingSourceOperationMask];
    pboard = [sender draggingPasteboard];
    if ( [[pboard types] containsObject:NSPasteboardTypeFileURL] ) {
        if (sourceDragMask & NSDragOperationLink) {
            return NSDragOperationLink;//拖动变成箭头
        } else if (sourceDragMask & NSDragOperationCopy) {
            return NSDragOperationCopy;//拖动会变成+号
        }
    }
    return NSDragOperationNone;
}

//当文件在界面中放手
-(BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender{
    NSPasteboard *zPasteboard = [sender draggingPasteboard];
    // 判断是否是单文件
    if (zPasteboard.pasteboardItems.count <= 1) {
        NSURL *url = [NSURL URLFromPasteboard:zPasteboard];
        if (url && self.delegate) {
            [self.delegate receivedFileUrl:url];
        }
    } else {
        //多文件
        NSArray *list = [zPasteboard propertyListForType:NSFilenamesPboardType];
        NSMutableArray *urlList = [NSMutableArray array];
        for (NSString *str in list) {
            NSURL *url = [NSURL fileURLWithPath:str];
            [urlList addObject:url];
        }
        if (urlList.count && self.delegate) {
            [self.delegate receivedFileUrlList:urlList];
        }
    }
    return YES;
}

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}
@end

读取文件的绝对路劲需要转换成NSURL获取path即可。

NSURL *url = [NSURL URLFromPasteboard:zPasteboard];
NSLog(@"%@",[url path]);

文件读取的方法,可以使用NSFileManager读取文件和文件信息。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.自定义控件 a.继承某个控件 b.重写initWithFrame方法可以设置一些它的属性 c.在layouts...
    圍繞的城阅读 8,815评论 2 4
  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 4,266评论 0 0
  • 刚才在刷微博时看到的,当时感觉他说的太对了。 我们活在这个世界上,本身就意味着价值。 不知你有没有读过余华先生的《...
    楚澹阅读 4,906评论 0 6
  • 很感动,感触颇多
    梦幻行空阅读 1,306评论 2 3
  • 夜半惊醒 四面黑冷的墙壁缓缓的压了过来 清冷的空气一丝一丝的渗入到每个毛孔 于是想起自己平庸的过往 时间像是被扭曲...
    仅仅一毫升阅读 1,014评论 0 0