简体   繁体   English

从Cocoa App拖放到Finder

[英]Drag and drop from Cocoa App to Finder

I have a NSCollectionView , that displays some files: 我有一个NSCollectionView ,它显示一些文件: 在此输入图像描述

What I want is... 我想要的是......

  1. being able to select one file. 能够选择一个文件。
  2. being able to drag this file and drop it on the desktop or another Finder window. 能够拖动此文件并将其放在桌面或其他Finder窗口上。

The file icons are displayed by a NSImageView. 文件图标由NSImageView显示。

What would be a good approach to provide such functionality? 提供此类功能的好方法是什么?

Thanks a lot! 非常感谢!

You need to call declareTypes:owner: on the collection view and pass in the pasteboard types that you want to be able to drag. 您需要在集合视图上调用declareTypes:owner:并传入您希望能够拖动的粘贴板类型。 In your case you want NSURLPboardType . 在您的情况下,您需要NSURLPboardType

All you need to do then is to implement the three dragging-related delegate methods in your NSCollectionView 's delegate . 您需要做的就是在NSCollectionView委托中实现三个与拖动相关的委托方法。

You should return YES from this method: 您应该从此方法返回YES

collectionView:canDragItemsAtIndexes:withEvent:

You can return a custom drag image for each item using this method: 您可以使用以下方法为每个项目返回自定义拖动图像:

collectionView:draggingImageForItemsAtIndexes:withEvent:offset:

And you write the items to the pasteboard using this method: 然后使用此方法将项目写入粘贴板:

collectionView:writeItemsAtIndexes:toPasteboard:

In your case, you would write the URL for the dragged item to the pasteboard in your implementation of this method. 在您的情况下,您将在此方法的实现中将拖动项目的URL写入粘贴板。

If you don't understand how to work with the pasteboard you should read the guide Drag and Drop Programming Topics . 如果您不了解如何使用粘贴板,则应阅读指南拖放编程主题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM