首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在iPhone上从collectionView拖放?

如何在iPhone上从collectionView拖放?
EN

Stack Overflow用户
提问于 2020-04-16 23:00:39
回答 1查看 160关注 0票数 0

我正在尝试在我为iPhone创建的应用程序中实现拖放功能,它不能从拖拽开始工作,因此我仍然没有尝试拖放。这对我来说是一个新特性,我正在尝试学习一些培训材料,所有这些材料都是为了某种原因而为iPad制作的。如果我尝试在iPhone或iPhone模拟器中运行此训练代码(从课程所有者处下载),它也不能正常工作。培训资料链接: 1. https://www.raywenderlich.com/3121851-drag-and-drop-tutorial-for-ios 2. https://medium.com/hackernoon/how-to-drag-drop-uicollectionview-cells-by-utilizing-dropdelegate-and-dragdelegate-6e3512327202

我的部分代码:

collectionView.dragDelegate = self

代码语言:javascript
复制
    func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
        return dragItems(at: indexPath)
    }

    private func dragItems(at indexPath: IndexPath) -> [UIDragItem] {
        if let itemCell = collectionView?.cellForItem(at: indexPath)
            as? CardCollectionViewCell,
            let image = itemCell.imageViewBack.image {
            let dragItem =
                UIDragItem(itemProvider: NSItemProvider(object: image))
            dragItem.localObject = indexPath//image
            return [dragItem]
        }   else {
            return []
        }
    }
} 
EN

回答 1

Stack Overflow用户

发布于 2020-04-17 01:28:28

您为NSItemProvider使用了错误的初始化器。该初始化器用于符合NSItemProviderWriting的类型。它应该是:

代码语言:javascript
复制
let dragItem = UIDragItem(itemProvider: NSItemProvider(item: image, typeIdentifier: kUTTypeImage as String))    
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61253479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档