Prevent NSCollectionView 'lifting' an item during drag

十年热恋 提交于 2020-04-11 08:32:07

问题


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?

from NSCollectionViewDelegate to ensure that

func collectionView(_ collectionView: NSCollectionView, draggingImageForItemsAt indexPaths: Set<IndexPath>, with event: NSEvent, offset dragImageOffset: NSPointPointer) -> NSImage

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.

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.

Dragging images in Photos.app and files in Finder.app (icon view) do not lift the item so hopefully this is possible.


回答1:


This appears to be essentially the same question as NSCollectionView - dragging shows a “hole” - want it to look like the Finder

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


来源:https://stackoverflow.com/questions/54467265/prevent-nscollectionview-lifting-an-item-during-drag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!