[SWIFT] Reorder cells in UICollectionView using drag & drop

Dejan Atanasov
HackerNoon.com
3 min readAug 1, 2017

--

In this tutorial, I will cover how to reorder cells in UICollectionView using drag & drop. I have seen many different approaches on how to do this, some are working decent and some are not. That's why I will show you the approach that is tested and working 100%.

To make this tutorial shorter, I will only present how to do the reordering and won't show all the required delegate methods. Since you got here, I count that you already know the UICollectionView basics.

UICollectionViewDelegate

We will start by using two delegate methods from the UICollectionViewDelegate protocol.

1 Enable the use of moving items in the UICollectionView by calling canMoveItemAt delegate method. Passing true will enable this feature.

2 Next, we will implement the moveItemAt delegate method where you will intercept the starting index and the ending index of the both items that are switching places.

UILongPressGestureRecognizer

For a better control of the gestures in the UICollectionView we will implement a UILongPressGestureRecognizer.

Note that these interactive functions work for iOS 9 and above.

That’s it from this tutorial and if it helped you please 👏 or share this story so others like you can find it. Thank you for your attention! 🚀

Check out my latest projects:

Read more of my writing on Medium:

Subscribe to my Newsletter:

--

--

Dejan Atanasov
HackerNoon.com

Senior iOS & Android Developer | Creator of Swift2Go | @hackernoon writer | Hire Me: http://bit.ly/2XfxbBR