gpt4 book ai didi

swift - 在拖动过程中防止 NSCollectionView 'lifting' 项目

转载 作者:行者123 更新时间:2023-12-03 16:40:07 25 4
gpt4 key购买 nike

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

我当前的解决方案是实现

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)似乎可以阻止拖动将项目从 Collection View 中“提升”,留下一个空的空间。

拖动 Photos.app 中的图像和 Finder.app(图标 View )中的文件不会抬起该项目,因此希望这是可能的。

最佳答案

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

我最初的解决方案是对 Collection View 项使用自定义 View 。在我的子类中,我重写 setHidden: 以防止 Collection View 在拖动项目时隐藏我的项目 View 。这有一些不良的副作用。看来 NSCollectionView 在更改布局时也会隐藏项目 View 。

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

@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

关于swift - 在拖动过程中防止 NSCollectionView 'lifting' 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54467265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com