【问题标题】:Accepting drag operations in an NSCollectionView subclass在 NSCollectionView 子类中接受拖动操作
【发布时间】:2010-05-02 16:39:03
【问题描述】:

我已将 NSCollectionView 子类化,并尝试从 Finder 接收拖动的文件。我收到draggingEntered: 并返回一个适当的值,但我从未收到prepareForDragOperation:(在此过程中也没有收到任何方法)。我这里有什么明显的遗漏吗?

代码:

- (void)awakeFromNib
{
    [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    NSLog(@"entered"); //Happens
    NSPasteboard *pboard;
    NSDragOperation sourceDragMask;

    sourceDragMask = [sender draggingSourceOperationMask];
    pboard = [sender draggingPasteboard];

    if ([[pboard types] containsObject:NSFilenamesPboardType])
    {
        NSLog(@"copy"); //Happens
        return NSDragOperationCopy;
    }

    return NSDragOperationNone;
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
    NSLog(@"prepare"); //Never happens
    return YES;
}

【问题讨论】:

    标签: objective-c cocoa macos drag-and-drop nscollectionview


    【解决方案1】:

    这已经很晚了,但我发现了问题:

    NSCollectionView 默默地提供了一个不兼容的实现:

    -(NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender
    

    ...Apple 没有记录这一点。如果您只是实现该方法以重新调用 draggingEntered 方法,则一切正常,例如:

    -(NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender
    {
        return [self draggingEntered:sender];
    }
    

    (我来到 SO 希望找到这个自定义实现提供的“魔法”的解释,因为这也是......未记录的(感谢 Apple!)。我猜它在管理插入方面做得很聪明-CollectionView 中的点?)。

    更新:似乎特殊的魔法在 NSCollectionView 的委托对象内部。出于某种原因,Xcode4 声称我没有委托,但分配它构建并运行正常。查看那里的所有自定义/半文档化拖放方法。

    (或者就像我上面描述的那样,覆盖自定义行为,并实现一些你能理解的工作)

    【讨论】:

      【解决方案2】:

      您可能想从 NSCollectionViewDelegate Protocol

      尝试这些委托方法
      - (NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id <NSDraggingInfo> )draggingInfo proposedIndex:(NSInteger *)proposedDropIndex dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation;
      - (BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id <NSDraggingInfo> )draggingInfo index:(NSInteger)index dropOperation:(NSCollectionViewDropOperation)dropOperation;
      
      - (BOOL)collectionView:(NSCollectionView *)collectionView canDragItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent *)event;
      - (NSImage *)collectionView:(NSCollectionView *)collectionView draggingImageForItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent *)event offset:(NSPointPointer)dragImageOffset;
      - (NSArray *)collectionView:(NSCollectionView *)collectionView namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropURL forDraggedItemsAtIndexes:(NSIndexSet *)indexes;
      - (BOOL)collectionView:(NSCollectionView *)collectionView writeItemsAtIndexes:(NSIndexSet *)indexes toPasteboard:(NSPasteboard *)pasteboard;
      

      特别是前两种方法。

      【讨论】:

      • 这些委托方法似乎只用于从集合视图中拖动,所以很遗憾,它们对我没有帮助。 (我从 Finder 中拖出)。但这可能确实说明了为什么它没有按预期工作......可能Apple让我的工作变得更加困难,以便更容易从-其他集合视图中拖动。
      • 似乎 NSCollectionViewDelegate 仅在 10.6 中可用,雪豹。
      【解决方案3】:

      我不久前经历过这个。这对我来说似乎违反直觉,但我可以让它工作的唯一方法是将关联的滚动视图设置为放置目标。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-08
        • 2017-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多