Reordering UICollectionView in iOS7

I want to create a UICollectionView or use something like this to create a grid in which I can rearrange cells. My application supports iOS 7 and, unfortunately, the UICollectionView method is (BOOL)beginInteractiveMovementForItemAtIndexPath:(NSIndexPath *)indexPath is only available on iOS 9 and later.

Is there any way to achieve the effect of "drag and reorder" collection view cells on iOS7? Thanks.

+5
source share
1 answer

Well, I had the same problem, so let me add my two cents to that.

To achieve the same effect that UIKit does when rearranging cells, I took a more or less similar approach, described below:

  • Add a long-press gesture recognizer in the form of a collection so that you can find out which cell was used using the collectionView:didSelectItemAtIndexPath .

  • When the user clicks on a cell, take a screenshot of that cell, add a snapshot view to the collection view, and then hide the cell. When the user moves his finger on the screen, refresh the center of the snapshots to move it programmatically so that it looks like the user is dragging a cell.

  • The trick is to decide when and how to change cells. When the snapshot moves, it will intersect with other cells. When this happens, you must update your data source and call moveItemAtIndexPath so that it replaces the position of the original cell and the cell that the snapshot intersects.

  • When dragging the ends, take a snapshot from the collection view and show the original cell.

You can correlate these steps to indicate the states of the resolvers, so that we know what you should do in each state:

  • Started : 1
  • Changed : 2, 3
  • Completed , Canceled , Failure : 4

I posted an example on my github account so you can download and play with it. Here I just implement the gesture callback:

Note: the code below may not work because I dropped some specific implementation information. But this should show the main intention. Download the full code from my github repository.

 func longPressRecognized(recognizer: UILongPressGestureRecognizer) { let location = recognizer.locationInView(collectionView) let indexPath = collectionView.indexPathForItemAtPoint(location) switch recognizer.state { case .Began: let cell = collectionView.cellForRowAtIndexPath(indexPath) let snapshotView = cell.snapshotViewAfterScreenUpdates(true) snapshot.center = cell.center collectionView.addSubview(snapshotView) cell.contentView.alpha = 0.0 // hides original cell case .Changed: snapshotView.center = location // will follow user finger. dataSource.swap(originalCellIndexPath.item, indexPath.item) // swaps data collectionView.moveItemAtIndexPath(originalCellIndexPath, toIndexPath: indexPath) // swaps cells originalCellIndexPath = indexPath default: let cell = cellForRowAtIndexPath(originalCellIndexPath) cell.contentView.alpha = 1.0 snapshotView.removeFromSuperview() } } 

Here is the effect you get (recorded on iOS 8.4 Simulator):

enter image description here

+9
source

Source: https://habr.com/ru/post/1247805/


All Articles