UICollectionViewCell的自定义视图拖动预览 [英] Custom View for UICollectionViewCell Drag Preview

查看:67
本文介绍了UICollectionViewCell的自定义视图拖动预览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to implement a feature where by a user drags and drops one collectionview cell onto another. However, I want to change the preview of the thing in motion completely, as to match the visual metaphor of my app (the item isn't moving, something the item contains is moving).

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

For example, say my collectionview cell shows a pen of pigs, and I want to to let the pig move from one pen to another, the preview view should be a view showing a single, not the pen. Its a slightly different use to what apple intended with their API, but a valid one I think.

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

I've seen func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters but that just lets you clip it slightly, not re-do the whole view.

有什么想法/想法吗?

推荐答案

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

Since iOS 11 you can make use of UIDragItem. There is a

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]
}

在您开始拖动拖动图像周围的项目时,它将立即更改为您的自定义视图.

As soon as you start dragging an item around the drag image will be changed to your custom view.

演示

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

In the demo you can see two UICollectionViews. A drag & drop operation is started from the upper UICollectionView and an item is dragged to the lower one. As the item moves, a custom preview of the item is displayed.

这是您要找的吗?

这篇关于UICollectionViewCell的自定义视图拖动预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆