NSCollectionView - dragging shows a “hole” - want it to look lke the Finder

放肆的年华 提交于 2020-02-21 05:38:42

问题


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.

[


回答1:


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


来源:https://stackoverflow.com/questions/50381431/nscollectionview-dragging-shows-a-hole-want-it-to-look-lke-the-finder

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