UICollectionView Reordering

I was very interested to read about UICollectionView’s newfound reordering ability in iOS 9, thanks to NSHint’s article on the subject. Helpfully all the example code for the post is available so I was able to download it and have quick play. A few thoughts about the functionality have struck me:

UICollectionView’s implementation — as best I can tell in poking and prodding — is based entirely around the centre-point of items.

As the collection view is told to update during dragging, it tests to see if another item lies at the target position. If so, it tries to move the dragged item to that new spot. Consequently, it is possible to drag purely over the gaps between items and never have a reorder actually kick in. I suspect in practical uses on-device, this probably won’t end up being an issue, but we’ll see.

Because of this focus on the centre of items, if you begin dragging from a corner of an item, it’ll then jump to be centred underneath your finger. Particularly for larger items, this could be a little unnerving. If so, it shouldn’t be too much trouble to track how far away from the centre the touch-down event was, and offset all positions to match that.

The drag model described above works best when your items are of uniform size. As the article demonstrates, non-uniform sizing is supported, but play with the demo and you might notice the occasional issue. The real trouble comes when moving a small item. I think a screen capture shows it best:

Interactively moving a small item

After a move, the layout changes to accommodate that. Consequently, there’s now a different item underneath the target point, which makes it seem like another move is in order. That second move ends up effectively undoing the first, with the overall result being the weird jitters seen above, as the two items repeatedly try to swap back and forth.

There’s a delegate method to decide if a move is allowed to happen, so I figure your code needs to get smart and decline certain moves until they can confidently go through. (or maybe the first move needs to be accepted, and then it’s the delegate’s job to stop that being mistakenly “undone”)

Finally, I hadn’t twigged before that UILongPressGestureRecognizers are continuos, so thanks for bringing that to my attention NSHint!

© Mike Abdullah 2007-2015