【问题标题】:Prevent NSCollectionView 'lifting' an item during drag防止 NSCollectionView 在拖动过程中“提升”项目
【发布时间】:2019-01-31 18:43:07
【问题描述】:

我了解如何为NSCollectionView 进行拖放操作,但我似乎无法阻止它从视图中“提升”项目。

我目前的解决方案是实施

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 调用,我可以在其中提供自己的拖动图像。但是,这些不会聚集或提供显示正在拖动的项目数量的图标。

问题是,当我实现前一种方法时,我似乎没有做任何事情(包括覆盖 NSCollectionViewItem 的draggingImageComponents)似乎阻止了将项目从集合视图中“提起”,留下一个空白空间。

在 Photos.app 中拖动图像和在 Finder.app(图标视图)中拖动文件不会提升项目,因此希望这是可能的。

【问题讨论】:

  • lldb + hopper 显示它是拖动imgur.com/a/BSBqOUz的自定义实现@
  • 哇!非常感谢您对此进行调查。我认为这意味着我不能用公共 API 做类似的事情?
  • 我正在检查具有 PVSidebarThumbnailController 的 Preview.app,您可以在其中执行相同操作。它只使用公共功能。所以是的,公众有可能。使用 Hopperapp 查看内部。 lldb 也可以设置断点(-n 或 -a)。还使用 po [NSUserDefaults standardUserDefaults] setValue:@(4) forKey:@"NSDragManagerLogLevel"] 为粘贴板(预览版)中的项目设置标志以调试类型。复制该功能需要太多时间。在 hopper 中查看 Apple 的伪代码应该不难理解发生了什么。 imgur.com/a/15VCVtI (Photos.app pasteboarditems)
  • 你解决了吗?
  • 没有。最后,我自己制作了一堆选定的项目。

标签: swift cocoa nscollectionview


【解决方案1】:

这似乎与NSCollectionView - dragging shows a “hole” - want it to look like the Finder本质上是同一个问题

我最初的解决方案是为集合视图项使用自定义视图。在我的子类中,我覆盖setHidden: 以防止在拖动项目时集合视图隐藏我的项目视图。这有一些不良的副作用。似乎 NSCollectionView 在更改布局时也会隐藏项目视图。

下一个想法(到目前为止)在我的应用程序中运行良好。我在拖动开始时取消隐藏项目视图。

@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

【讨论】:

    猜你喜欢
    • 2019-01-08
    • 2017-10-02
    • 2011-01-30
    • 2018-07-23
    • 2014-12-16
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多