2
votes

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.

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.

Is there a (hopefully easy) way to get NSCollectionView to act more like the Finder when dragging? Thanks.

[The current behavior - not desired[1]

1

1 Answers

1
votes

This appears to be essentially the same question as Prevent NSCollectionView 'lifting' an item during drag

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