防止在拖动过程中NSCollectionView提起项目 [英] Prevent NSCollectionView 'lifting' an item during drag
问题描述
我知道如何在 NSCollectionView
中进行拖放,但是我似乎无法阻止它将项目从视图中移出。
I understand how to get drag and drop working for NSCollectionView
but I can't seem to stop it 'lifting' the items off the view.
我当前的解决方案是不实施
My current solution is to not implement
func collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting?
来自 NSCollectionViewDelegate
,以确保
func collectionView(_ collectionView: NSCollectionView, draggingImageForItemsAt indexPaths: Set<IndexPath>, with event: NSEvent, offset dragImageOffset: NSPointPointer) -> NSImage
IS ,我可以在其中提供自己的拖动图像。但是,这些不会聚集或提供显示正在拖动多少项目的图标。
IS called, where I can provide my own dragging images. However, these do not flock or provide the icon showing how many items are being dragged.
问题是,当我实现前一种方法时,我似乎无能为力(包括覆盖NSCollectionViewItem的 draggingImageComponents
)似乎可以防止将项目从集合视图中拖动出来,留下一个空白。
The problem is that when I implement the former method, nothing I seem to do (including overriding the NSCollectionViewItem's draggingImageComponents
) seems to prevent the drag 'lifting' the item off the collection view, leaving behind an empty space.
拖动Photos.app中的图像和Finder.app中的文件(图标视图)不会抬起该项目,因此希望可以这样做。
Dragging images in Photos.app and files in Finder.app (icon view) do not lift the item so hopefully this is possible.
推荐答案
这似乎与 NSCollectionView-拖动显示一个洞-希望它看起来像Finder
我最初的解决方案是对集合视图使用自定义视图项目。在我的子类中,我重写了 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提起项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!