我有一个 NSCollectionView。当我拖动项目时(在 View 周围或 View 外),我得到了正确的外观,但是在拖动发生时单元格是空的(参见下面的动画 GIF)。所需的行为就像 Finder 一样 - 即,图标应保留在原位,并且拖动的图像会出现在它旁边。
我尝试了很多方法都无济于事。例如,我尝试在 sourceOperationMaskForDraggingContext 中将拖动操作设置为 Copy。不记得其他的——他们失败了所以我删除了实验。拖动完成后,一切都会正确更新,但在拖动过程中看起来不对。
是否有一种(希望很简单)方法可以让 NSCollectionView 在拖动时表现得更像 Finder?谢谢。
[
最佳答案
这似乎与 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/