Cancel active drag in UICollectionView

Viewed 1229

I've implemented UICollectionViewDragDelegate to allow the user to drag and drop collection view cells. I also have a UILongPressGestureRecognizer added to each cell. Upon long pressing the cell, the drag interaction is recognized before the long press gesture state begins. If you drag it around it cancels the long press gesture. This works pretty well.

But to make these two play nicer together, when the long press gesture is recognized (state == .began) I would like to programmatically cancel the ongoing drag session - cancel it as if the user released their finger causing the cell to no longer be dragged around, but continue recognizing the long press gesture. Otherwise the drag and the long press action are both activated which doesn't work well in my case - one or the other needs to be activated at a time. How can that be done?

Note that if you continue long pressing the cell without dragging, the drag will cancel after about 2 seconds. I simply want to cancel it as soon as the long press gesture is recognized so the user doesn’t have to wait.

UICollectionView has a property hasActiveDrag to indicate that status, but I'm not seeing a way to cancel it. I did try setting dragInteractionEnabled to false and then true again hoping that would cancel it but it remains active. I even tried reloading the cell at the index path that's being dragged but that didn't cancel it either.

I've put together a little Sample Project you can run to see what's going on here.

1 Answers
Related