-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathMRDragView.m
More file actions
88 lines (77 loc) · 2.16 KB
/
MRDragView.m
File metadata and controls
88 lines (77 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// MRDragView.m
// FFmpegTutorial-macOS
//
// Created by Matt Reach on 2020/12/2.
//
#import "MRDragView.h"
@implementation MRDragView
- (void)registerDragTypes
{
if (@available(macOS 10.13, *)) {
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSPasteboardTypeFileURL, nil]];
} else if (@available(macOS 10.0, *)){
// Fallback on earlier versions
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
}
}
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self) {
//注册文件拖动事件
[self registerDragTypes];
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self registerDragTypes];
}
- (void)dealloc
{
[self unregisterDraggedTypes];
}
- (NSArray *)draggedFileList:(id<NSDraggingInfo> _Nonnull)sender
{
NSPasteboard *pboard = [sender draggingPasteboard];
NSArray *list = nil;
if (@available(macOS 10.13, *)) {
if ([[pboard types] containsObject:NSPasteboardTypeFileURL]) {
list = [pboard readObjectsForClasses:@[[NSURL class]] options:nil];
}
} else {
if ([[pboard types] containsObject:NSFilenamesPboardType]) {
list = [pboard propertyListForType:NSFilenamesPboardType];
}
}
NSMutableArray *result = [NSMutableArray arrayWithArray:list];
for (int i = 0; i < [result count]; i ++) {
id obj = result[i];
if ([obj isKindOfClass:[NSString class]]) {
obj = [NSURL fileURLWithPath:(NSString *)obj];
result[i] = obj;
}
}
return [result copy];
}
//当文件被拖动到界面触发
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
NSArray * list = [self draggedFileList:sender];
if (self.delegate) {
return [self.delegate acceptDragOperation:list];
}
return NSDragOperationNone;
}
//当文件在界面中放手
- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender
{
NSArray * list = [self draggedFileList:sender];
if (list.count && self.delegate) {
[self.delegate handleDragFileList:list];
}
return YES;
}
@end