您的視圖或窗口需要註冊接收拖動的紙板類型你有興趣:
[self registerForDraggedTypes:@[(id)kUTTypeURL]];
拖動時,您將獲得一個第一回調進入查看:
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)info
{
NSPasteboard *draggingPasteboard = [info draggingPasteboard];
// NSDragOperation draggingSourceOperationMask = [sender draggingSourceOperationMask];
NSArray *classes = @[[NSURL class]];
NSDictionary *options = @{NSPasteboardURLReadingFileURLsOnlyKey : @YES};
if ([draggingPasteboard canReadObjectForClasses:classes options:options]) {
// NSArray *fileURLs = [draggingPasteboard readObjectsForClasses:classes
// options:options];
//
// if ([fileURLs count] != 1) {
// return NSDragOperationNone;
// }
return NSDragOperationLink;
}
return NSDragOperationNone;
}
如果您的視圖拖動結束(例如,文件被刪除),你會得到另一個回調在那裏你可以從剪貼板和PROC閱讀ESS您收到的項目:
- (BOOL)performDragOperation:(id <NSDraggingInfo>)info
{
NSPasteboard *draggingPasteboard = [info draggingPasteboard];
if ([draggingPasteboard canReadObjectForClasses:[NSArray arrayWithObject:[NSURL class]] options:nil]) {
return [self addItemWithDragInfo:info];
}
return NO;
}
- (BOOL) addItemWithDragInfo:info:(id <NSDraggingInfo>)info
{
NSPasteboard *draggingPasteboard = [info draggingPasteboard];
NSArray *classes = @[[NSURL class]];
NSDictionary *options = @{NSPasteboardURLReadingFileURLsOnlyKey : @YES};
NSArray *fileURLs = [draggingPasteboard readObjectsForClasses:classes options:options];
// TODO: Do something with those files…
return YES;
}
BTW,LaunchPad的可能使用NSCollectionView或IKImageBrowserView。這些視圖已經包含了一些對拖放的支持。
你確定NSCollectionView對我有幫助嗎? – 2014-09-12 06:43:41
我在Mac應用程序中使用IKImageBrowserView(HoudahSpot,HoudahGeo和Tembo - http://www.hoduah.com)。我記得反覆評估NSCollectionView。不幸的是,我一直忘記爲什麼每次我最終都喜歡通過NSCollectionView的IKImageBrowserView。 – 2014-09-12 08:46:37
有沒有可用的選項來進行摸索?就像LaunchPad中的Utilities文件夾一樣。 – 2014-09-12 09:00:47