NSCollectionView - 拖动显示 "hole" - 希望它看起来像 Finder

NSCollectionView - dragging shows a "hole" - want it to look lke the Finder

我有一个 NSCollectionView。当我拖动项目时(在视图周围或视图外),我得到了正确的外观,但是在拖动发生时单元格是空的(参见下面的动画 GIF)。期望的行为是像 Finder - 也就是说,图标应该保留在原位,并且除了它之外还显示拖动的图像。

我尝试了很多方法都无济于事。例如,我尝试在 sourceOperationMaskForDraggingContext 中将拖动操作设置为 Copy。不记得其他的——他们失败了所以我删除了实验。拖动完成后,一切都正确更新,但在拖动过程中看起来不对。

有没有一种方法(希望很简单)让 NSCollectionView 在拖动时表现得更像 Finder?谢谢

[

这似乎与 Prevent NSCollectionView 'lifting' an item during drag

本质上是同一个问题

我最初的解决方案是为集合视图项使用自定义视图。在我的子类中,我重写 setHidden: 以防止集合视图在拖动项目时隐藏我的项目视图。这有一些不良的副作用。看起来 NSCollectionView 在更改布局时也隐藏了项目视图。

下一个想法(到目前为止)在我的应用程序中运行良好。我在拖动开始时取消隐藏项目视图。

@interface HGImagesCollectionViewDelegate : NSObject <NSCollectionViewDelegate>

@end

@implementation HGImagesCollectionViewDelegate

- (void)collectionView:(NSCollectionView *)collectionView
       draggingSession:(NSDraggingSession *)session
      willBeginAtPoint:(NSPoint)screenPoint
  forItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths
{
    //  Prevent item from being hidden (creating a hole in the grid) while being dragged
    for (NSIndexPath *indexPath in indexPaths) {
        [[[collectionView itemAtIndexPath:indexPath] view] setHidden:NO];
    }

    // …

}  

@end