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

标签 macos drag-and-drop nscollectionview

我有一个 NSCollectionView。当我拖动项目时(在 View 周围或 View 外),我得到了正确的外观,但是在拖动发生时单元格是空的(参见下面的动画 GIF)。所需的行为就像 Finder 一样 - 即,图标应保留在原位,并且拖动的图像会出现在它旁边。

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

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

[ The current behavior - not desired[1]

最佳答案

这似乎与 Prevent NSCollectionView 'lifting' an item during drag 本质上是同一个问题

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

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

@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

关于macos - NSCollectionView - 拖动显示 "hole"- 希望它看起来像 Finder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50381431/

相关文章:

swift - NSCollectionViewItem 在 itemForRepresentedObjectAtIndexPath 中没有导出

objective-c - OSX10.5 之前版本中 NSCollectionView 的替代方案,Cocotron?

cocoa - 基于 View 的 NSTableView 与 NSCollectionView?

python - 使用 Tkinter 在 python 中的选项菜单中添加分隔符

objective-c - 处理 NSStatusItem 上左键和右键单击时出现问题

c++ - 某些相关文件类型(.zip、.rar、...)的文件删除机制

android - 通过拖放android将项目添加到可扩展 ListView

cocoa - 在其他 View 上拖动时未释放 MouseDragged

windows - 是否可以将 Autohotkey 与 Apple Mac 键盘一起使用,将 OSX 键盘快捷键转换为 PC 上的 Windows 7 等效快捷键?

linux - 从网站下载文件时保留文件服务器的文件原始创建/修改日期