问题描述
我有一个NSCollectionView.当我拖动项目时(在视图周围或视图外部),我得到了正确的外观,但是在发生拖动时该单元格为空(请参见下面的动画GIF).所需的行为就像Finder一样-也就是说,图标应保留在原处,并且已拖动的图像也将显示在其中.
I have an NSCollectionView. When I drag items (around the view, or outside of the view), I get the right appearance, but the cell is empty while the dragging occurs (see animated GIF below). The desired behavior is to be like the Finder - that is, the icon should remain in place and the dragged image appears in addition to it.
我尝试了各种各样的事情都徒劳无功.例如,我尝试将拖动操作设置为sourceOperationMaskForDraggingContext中的Copy.不记得其他人了-他们失败了,所以我删除了实验.拖动完成后,所有内容都会正确更新,但是在拖动过程中,它看起来是错误的.
I have tried a variety of things to no avail. For example, I tried setting the drag operation to Copy in sourceOperationMaskForDraggingContext. Can't remember the other ones - they failed so I deleted the experiments. When the drag completes, everything updates properly, but during the drag it looks wrong.
是否有一种(希望很简单的方法)使NSCollectionView在拖动时更像Finder?谢谢.
Is there a (hopefully easy) way to get NSCollectionView to act more like the Finder when dragging? Thanks.
[
推荐答案
这似乎与
我最初的解决方案是对集合视图项使用自定义视图.在我的子类中,我重写了setHidden:
以防止在拖动项目时集合视图隐藏我的项目视图.这有一些不良的副作用.看起来,NSCollectionView在更改布局时也会隐藏项目视图.
My initial solution was to use a custom view for the collection view item. In my subclass I override setHidden:
to prevent the collection view from hiding my item view while the item is being dragged. This had some undesirable side-effects. It appears that NSCollectionView also hides item views when changing the layout.
下一个想法(到目前为止)在我的应用程序中运行良好.开始拖动时,我将取消隐藏项目视图.
The next idea (so far) works fine in my application. I un-hide the item views when dragging starts.
@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
这篇关于NSCollectionView-拖动显示一个“空" -希望它看起来像Finder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!