6

我有一个绑定到 NSArrayController 的 NSCollectionView。我想让拖放工作,所以我创建一个委托并实现方法

-(BOOL)collectionView:(NSCollectionView *)collectionView canDragItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent*)event
-(BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id < NSDraggingInfo >)draggingInfo index:(NSInteger)index dropOperation:(NSCollectionViewDropOperation)dropOperation
-(NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id < NSDraggingInfo >)draggingInfo proposedIndex:(NSInteger *)proposedDropIndex dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation
-(NSArray *)collectionView:(NSCollectionView *)collectionView namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropURL forDraggedItemsAtIndexes:(NSIndexSet *)indexes

我为两个 BOOL 方法返回 YES,为 validateDrop: 方法返回 NSDragOperationMove,为 namesOfPromisedFilesDroppedAtDestination: 方法返回一个空数组。我还有一个 NSLog 语句作为每个方法的第一行,所以我可以看到它们何时被调用。

现在,唯一被调用的方法是 canDragItemsAtIndexes: (我返回 YES)。我看到它被调用了,但是任何进一步的拖动只会修改选择。其余的永远不会被调用。

如果我使 NSCollectionView 不支持选择,那么甚至不会调用该方法。

我确定我错过了一些非常明显的东西,但我无法弄清楚它是什么。有没有人使用 NSCollectionViews 进行拖放并且可以阐明一些问题?

4

2 回答 2

4

我认为您错过了将拖动内容写入粘贴板的部分。
要支持拖放,您必须执行以下步骤:

  1. 确定是否可以拖动拖动源
  2. 如果YES,将内容写入粘贴板
  3. 验证并接受放置目标中的项目

写入粘贴板应该在
- collectionView:writeItemsAtIndexes:toPasteboard:

您还必须使用以下方式注册您拖动的类型- registerForDraggedTypes:

一些示例代码:http: //developer.apple.com/library/mac/#samplecode/IconCollection/Introduction/Intro.html

于 2011-04-29T08:33:52.000 回答
0

这段代码包含了将图像从一个 NSCollectionView 拖到另一个所需的一切。弄清楚这一点并不是很明显。为源集合视图检查 Selectable 并为 dataSource 和委托出口连接,但我不需要 registerForDraggedTypes。

class Window:NSWindow, NSComboBoxDelegate, NSTextFieldDelegate, NSDatePickerCellDelegate, NSTableViewDataSource, NSTableViewDelegate, MKMapViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegate, NSCollectionViewDelegateFlowLayout, NSTabViewDelegate, NSMenuDelegate, NSDraggingDestination { }

    func collectionView(collectionView: NSCollectionView, writeItemsAtIndexPaths indexPaths: Set<NSIndexPath>, toPasteboard pasteboard: NSPasteboard) -> Bool {
    let index = indexPaths.first!.item
    let url = webImageURLs[index]   // array of string URLs that parallels the collection view.
    NSPasteboard.generalPasteboard().clearContents()
    NSPasteboard.generalPasteboard().declareTypes([kUTTypeText as String, kUTTypeData as String], owner: nil)
    NSPasteboard.generalPasteboard().setString(url, forType: (kUTTypeText as String))
    NSPasteboard.generalPasteboard().setData(webImageData[index], forType: (kUTTypeData as String))
    return true
}

// Provide small version of image being dragged to accompany mouse cursor.
func collectionView(collectionView: NSCollectionView, draggingImageForItemsAtIndexPaths indexPaths: Set<NSIndexPath>, withEvent event: NSEvent, offset dragImageOffset: NSPointPointer) -> NSImage {
    let item = collectionView.itemAtIndex(indexPaths.first!.item)
    return (item?.imageView?.image)!.resizeImage(20, height: 20)
}

// Image is dropped on destination NSCollectionView.
func collectionView(collectionView: NSCollectionView, draggingSession session: NSDraggingSession, endedAtPoint screenPoint: NSPoint, dragOperation operation: NSDragOperation) {
    let pasteboardItem = NSPasteboard.generalPasteboard().pasteboardItems![0]
    let urlString = pasteboardItem.stringForType((kUTTypeText as String))
    let imageData = pasteboardItem.dataForType((kUTTypeData as String))

    // destinationImages is the data source for the destination collectionView. destinationImageURLs is used to keep track of the text urls.
    if urlString != nil {
        destinationImageURLs.insert(urlString!, atIndex: 0)
        destinationImages.insert(NSImage(data: imageData!)!, atIndex: 0)
        destinationCollectionView.reloadData()
        let selectionRect = self.favoritesCollectionView.frameForItemAtIndex(0)
        destinationCollectionView.scrollRectToVisible(selectionRect)
    }
}

extension NSImage {
    func resizeImage(width: CGFloat, height: CGFloat) -> NSImage {
        let img = NSImage(size: CGSizeMake(width, height))
        img.lockFocus()
        let ctx = NSGraphicsContext.currentContext()
        ctx?.imageInterpolation = .High
        drawInRect(NSRect(x: 0, y: 0, width: width, height: height), fromRect: NSRect(x: 0, y: 0, width: size.width, height: size.height), operation: .CompositeCopy, fraction: 1)
        img.unlockFocus()

        return img
    }
} 
于 2016-01-27T15:16:52.990 回答