nscollectionViewドラッグアンドドロップ:呼び出されていないほとんどのデリゲートイベント

StackOverflow https://stackoverflow.com/questions/5825833

質問

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

2つのBoolメソッドについてはYESを返します。ValidateDrop:メソッドのnsdragoperationMove、およびdroppedatdestination:メソッドの名前の名前の空の配列です。また、各メソッドの最初の行としてnslogステートメントがあるので、呼び出されたときがわかります。

現在、呼び出される唯一の方法はcandRagitemsatindexesです:(私が返す場所)。私はそれが呼ばれることがわかりますが、さらにドラッグすると選択が変更されるだけです。残りは決して呼ばれません。

nscollectionViewが選択の選択をサポートしないようにすると、その方法さえ呼び出されません。

私は非常に明白なものが足りないと確信していますが、それが何であるかを理解することはできません。 nscollectionViewsを使用してドラッグアンドドロップしてドラッグアンドドロップして、光を放つことができますか?

役に立ちましたか?

解決

ドラッグコンテンツをペーストボードに書き込む部分が恋しいと思います。
ドラッグアンドドロップをサポートするには、次の手順を実行する必要があります。

  1. ドラッグソースでドラッグできるかどうかを判断します
  2. もしも YES, 、コンテンツをペーストボードに書き込みます
  3. ドロップターゲットのアイテムを検証して受け入れます

ペーストボードへの書き込みは、に実装する必要があります
- collectionView:writeItemsAtIndexes:toPasteboard:

また、ドラッグされたタイプを登録する必要があります - registerForDraggedTypes:

一部のサンプルコード:http://developer.apple.com/library/mac/#samplecode/iconcollection/introduction/intro.html

他のヒント

このコードには、あるnscollectionViewから別のnscollectionviewに画像をドラッグするために必要なものがすべて揃っています。これを理解することはあまり明白ではありませんでした。 Selectableはソースコレクションビューがチェックされ、DataSourceおよびDelegateアウトレット用に配線されていますが、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
    }
} 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top