UICollectionViewCell的自定义视图拖动预览

Sam*_*man 3 drag-and-drop ios uicollectionview

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

例如,假设我的collectionview单元显示了一支猪笔,而我想让猪从一支笔移动到另一只笔,则预览视图应该是只显示一只而不是一支笔的视图。这与苹果使用其API的意图稍有不同,但我认为这是有效的。

我已经看到过,func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters但这只是让您稍微裁剪一下,而不是重新制作整个视图。

有什么想法/想法吗?

Ste*_*cht 5

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

open var previewProvider: (() -> UIDragPreview?)?
Run Code Online (Sandbox Code Playgroud)

一个简单的例子:

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]
}
Run Code Online (Sandbox Code Playgroud)

一旦开始拖动,拖动图像周围的项目将更改为自定义视图。

演示版

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

在此处输入图片说明

那是您要找的东西吗?

  • 如何在提升时(我的意思是在拖动之前)自定义它 (2认同)