UICollectionViewCell拖动预览的自定义视图

问题描述 投票:1回答:1

我正在尝试实现一个功能,用户将一个collectionview单元拖放到另一个上。但是,我想完全改变运动中的事物的预览,以匹配我的应用程序的视觉隐喻(项目不移动,项目包含的东西正在移动)。

例如,假设我的collectionview单元格显示了一支猪,我想让猪从一支笔移动到另一支笔,预览视图应该是一个显示单个而不是笔的视图。它与苹果公司的API用途略有不同,但我觉得这是一个有效的用途。

我见过func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters,但这只是让你轻微剪辑,而不是重新做整个视图。

任何想法?/想法?

ios uicollectionview drag-and-drop
1个回答
4
投票

从iOS 11开始,您可以使用UIDragItem。有一个

open var previewProvider: (() -> UIDragPreview?)?

一个简单的例子:

func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem]
{
    ...
    let dragItem: UIDragItem = ....
    dragItem.localObject = item
    dragItem.previewProvider = { () -> UIDragPreview? in
        let imageView = UIImageView(image: UIImage(named: "preivewOfThingInMotion"))
        imageView.frame = CGRect(x: 0, y: 0, width: 64, height: 64)
        return UIDragPreview(view: imageView)
    }
    return [dragItem]
}

只要您开始拖动拖动图像周围的项目,就会更改为自定义视图。

演示

在演示中,您可以看到两个UICollectionViews。从上部UICollectionView开始拖放操作,并将项目拖动到下部。项目移动时,将显示项目的自定义预览。

enter image description here

那是你在找什么?

© www.soinside.com 2019 - 2024. All rights reserved.