快速拖动项目而不按住(触摸和拖动)4

时间:2018-12-17 05:19:51

标签: ios swift uicollectionview drag-and-drop

我实现了CollectionView的拖放操作,因此可以移动集合中的单元格。当我拖动一个单元格时,我只想触摸该单元格一次或将其保持很短的射击时间即可进行拖放。如何更改保持单元格以启用拖放所需的时间?

@IBOutlet weak var colectionViewOne: UICollectionView! {
    didSet{
        colectionViewOne.dataSource = self
        colectionViewOne.delegate = self
        colectionViewOne.dragDelegate = self
        colectionViewOne.dropDelegate = self
        colectionViewOne.dragInteractionEnabled = true     
    }
}


   func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
    let destinationIndexPath = coordinator.destinationIndexPath ?? IndexPath(item: 0, section: 0)
    for item in coordinator.items {
        if let sourceIndexPath = item.sourceIndexPath {
            if let attributedString = item.dragItem.localObject as? NSAttributedString {
                collectionView.performBatchUpdates({
                    temporaryModel.remove(at: sourceIndexPath.item)
                    temporaryModel.insert(attributedString.string, at: destinationIndexPath.item)
                    collectionView.deleteItems(at: [sourceIndexPath])
                    collectionView.insertItems(at: [destinationIndexPath])
                })
                coordinator.drop(item.dragItem, toItemAt: destinationIndexPath)
            }
        }

    }
}

func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
    return session.canLoadObjects(ofClass: NSAttributedString.self)
}

func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
    let isSelf = (session.localDragSession?.localContext as? UICollectionView) == collectionView
    return UICollectionViewDropProposal(operation: isSelf ? .move : .copy, intent: .insertAtDestinationIndexPath)
}


func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
    session.localContext = collectionView
    return dragItem(at: indexPath)
}

func dragItem(at indexpath: IndexPath) -> [UIDragItem]{
    if let attributedString = ( colectionViewOne.cellForItem(at: indexpath) as? PracticeDragDropCellCollectionViewCell)?.cellTextLabel.attributedText{
        let dragItem = UIDragItem(itemProvider: NSItemProvider(object: attributedString))
        dragItem.localObject = attributedString
        return [dragItem]
    }
    else {
        return []
    }
}

0 个答案:

没有答案