Use of CollectionViewDataSource method moveItemAt for Reordering CollectionView cell In IOS (Swift 4)

In this tutorial we are study about how we can reordered the collectionView cell in collectionView. This functionality is introduced in IOS 9, And this is used by adding the gesture on collectionView. Lest go we are study about this.

addGestureRecognizer with CollectionView

First we need to add gesture (Long press gesture) with collectionView for move the collection View item. Below code is for add the Long press geture :-

    var arrayCellTitle = [Int]()
 override func viewDidLoad() {
        super.viewDidLoad()
        for value in 0...20 {
            arrayCellTitle.append(value)
        }
        configureCollectionView()
    }
    
func configureCollectionView() {
        let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(actionLongPressGesture(gesture:)))
        self.collectionView.addGestureRecognizer(longPressGesture)
    }
    
    @objc func actionLongPressGesture(gesture: UILongPressGestureRecognizer) {
        
        switch(gesture.state) {
        case .began:
            guard let selectedIndexPath = self.collectionView.indexPathForItem(at: gesture.location(in: self.collectionView)) else {
                break
            }
            collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
        case .changed:
            collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
        case .ended:
            collectionView.endInteractiveMovement()
        default:
            collectionView.cancelInteractiveMovement()
        }
    }
  1. beginInteractiveMovementForItem(at indexPath: IndexPath) -> Its initiates the interactive movement of the cell at the specified index path.
  2. updateInteractiveMovementTargetPosition(_:) -> Update the item position whith In collection view and according to getsure position
  3. endInteractiveMovement()-> Its use for end interactive movement of item. After completed the long press gesture effect.
  4. cancelInteractiveMovement()-> Ends interactive movement tracking

 

Now, When we are move the collectionView item position we Are getting the call back in collectionViewDataSource method “moveItemAt”. We can manage here our data according to updated cell. Below are example of UICollectionViewDataSource “moveItemAt” method :

func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        let value = arrayCellTitle.remove(at: sourceIndexPath.item)
        arrayCellTitle.insert(value, at: destinationIndexPath.item)
    }

Handle Cell is move or not

In UICollectionViewDataSource provide a function “canMoveItemAt” for handle the item is move or not. In this method we need to return bool value for item is move or not.
Example :-

func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
        if indexPath.row % 2 == 0 {
            return true
        } else {
            return false
        }
    }

How to move item at particular indexPath

In UICollectionViewDelegate provide functions “targetIndexPathForMoveFromItemAt” for handle item target indexPath.

extension ViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath, toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath {
return IndexPath(item: 5, section: 0)
}
//customize the content offset to be applied during transition or update animations
func collectionView(_ collectionView: UICollectionView, targetContentOffsetForProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
return CGPoint(x: 100, y: 100)
}
}

 




You can download the complete Sample project here…

Related Post

Drag and Drop Table View cell in IOS 11 & swi... Drag and Drop Table View cell in IOS 11 & swift 4
Variables in Java Variables in Java A variable is something which is associated with a value can be changed. For example: int i=10; Here variable name “i” wh...
Java Operators Java Operators An operator is used to perform an operation over one or more operands. It is a character that represents an action, for example * is a...
Decision Control Statement C has three major decision making instructions— the if statement, the if-else statement, and the switch statement. The if Statement It is used to ex...
Array An array is a collection of similar kind of data elements stored in adjacent memory locations and are referred to by a single array-name. The first el...
Create UITableView (Table View) By the use of stor... How to use UITableView in Above IOS 8 and Swift (UITableView) In this tutorial we are study how to use the TableView in Swift. One of the most common...
Author

Write A Comment