簡體   English   中英

如何從 iPhone 上的 collectionView 拖放?

[英]How to drag and drop from collectionView on iPhone?

我正在嘗試在我專為 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 []
        }
    }
} 

您為NSItemProvider使用了錯誤的初始化程序。 該初始化程序適用於符合NSItemProviderWriting的類型。 它應該是:

let dragItem = UIDragItem(itemProvider: NSItemProvider(item: image, typeIdentifier: kUTTypeImage as String))    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM