我正在尝试在我为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
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 []
}
}
} 发布于 2020-04-17 01:28:28
您为NSItemProvider使用了错误的初始化器。该初始化器用于符合NSItemProviderWriting的类型。它应该是:
let dragItem = UIDragItem(itemProvider: NSItemProvider(item: image, typeIdentifier: kUTTypeImage as String)) https://stackoverflow.com/questions/61253479
复制相似问题