Custom Cell Reorder Behavior in CollectionView

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

I am able to reorder my collectionView like so:

However, instead of all cells shifting horizontally, I would just like to swap with the following behavior (i.e. with less shuffling of cells):

I have been playing with the following delegate method:

func collectionView(_ collectionView: UICollectionView, targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath, toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath

however, I am unsure how I can achieve custom reordering behavior.

回答1:

I managed to achieve this by creating a subclass of UICollectionView and adding custom handling to interactive movement. While looking at possible hints on how to solve your issue, I've found this tutorial : http://nshint.io/blog/2015/07/16/uicollectionviews-now-have-easy-reordering/. The most important part there was that interactive reordering can be done not only on UICollectionViewController. The relevant code looks like this :

var longPressGesture : UILongPressGestureRecognizer!  override func viewDidLoad() {     super.viewDidLoad()      // rest of setup              longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.handleLongGesture(_:)))     self.collectionView?.addGestureRecognizer(longPressGesture)  }  func handleLongGesture(gesture: UILongPressGestureRecognizer) {      switch(gesture.state) {      case UIGestureRecognizerState.Began:         guard let selectedIndexPath = self.collectionView?.indexPathForItemAtPoint(gesture.locationInView(self.collectionView)) else {             break         }         collectionView?.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath)     case UIGestureRecognizerState.Changed:         collectionView?.updateInteractiveMovementTargetPosition(gesture.locationInView(gesture.view!))     case UIGestureRecognizerState.Ended:         collectionView?.endInteractiveMovement()     default:         collectionView?.cancelInteractiveMovement()     } }

This needs to be inside your view controller in which your collection view is placed. I don't know if this will work with UICollectionViewController, some additional tinkering may be needed. What led me to subclassing UICollectionView was realisation that all other related classes/delegate methods are informed only about the first and last index paths (i.e. the source and destination), and there is no information about all the other cells that got rearranged, so It needed to be stopped at the core.

SwappingCollectionView.swift :

import UIKit  extension UIView {     func snapshot() -> UIImage {         UIGraphicsBeginImageContext(self.bounds.size)         self.layer.renderInContext(UIGraphicsGetCurrentContext()!)         let image = UIGraphicsGetImageFromCurrentImageContext()         UIGraphicsEndImageContext()         return image     } }  extension CGPoint {     func distanceToPoint(p:CGPoint) -> CGFloat {         return sqrt(pow((p.x - x), 2) + pow((p.y - y), 2))     } }  struct SwapDescription : Hashable {     var firstItem : Int     var secondItem : Int      var hashValue: Int {         get {             return (firstItem * 10) + secondItem         }     } }  func ==(lhs: SwapDescription, rhs: SwapDescription) -> Bool {     return lhs.firstItem == rhs.firstItem && lhs.secondItem == rhs.secondItem }  class SwappingCollectionView: UICollectionView {      var interactiveIndexPath : NSIndexPath?     var interactiveView : UIView?     var interactiveCell : UICollectionViewCell?     var swapSet : Set = Set()     var previousPoint : CGPoint?      static let distanceDelta:CGFloat = 2 // adjust as needed      override func beginInteractiveMovementForItemAtIndexPath(indexPath: NSIndexPath) -> Bool {          self.interactiveIndexPath = indexPath          self.interactiveCell = self.cellForItemAtIndexPath(indexPath)          self.interactiveView = UIImageView(image: self.interactiveCell?.snapshot())         self.interactiveView?.frame = self.interactiveCell!.frame          self.addSubview(self.interactiveView!)         self.bringSubviewToFront(self.interactiveView!)          self.interactiveCell?.hidden = true          return true     }      override func updateInteractiveMovementTargetPosition(targetPosition: CGPoint) {          if (self.shouldSwap(targetPosition)) {              if let hoverIndexPath = self.indexPathForItemAtPoint(targetPosition), let interactiveIndexPath = self.interactiveIndexPath {                  let swapDescription = SwapDescription(firstItem: interactiveIndexPath.item, secondItem: hoverIndexPath.item)                  if (!self.swapSet.contains(swapDescription)) {                      self.swapSet.insert(swapDescription)                      self.performBatchUpdates({                         self.moveItemAtIndexPath(interactiveIndexPath, toIndexPath: hoverIndexPath)                         self.moveItemAtIndexPath(hoverIndexPath, toIndexPath: interactiveIndexPath)                         }, completion: {(finished) in                             self.swapSet.remove(swapDescription)                                                             self.dataSource?.collectionView(self, moveItemAtIndexPath: interactiveIndexPath, toIndexPath: hoverIndexPath)                             self.interactiveIndexPath = hoverIndexPath                      })                 }             }         }          self.interactiveView?.center = targetPosition         self.previousPoint = targetPosition     }      override func endInteractiveMovement() {         self.cleanup()     }      override func cancelInteractiveMovement() {         self.cleanup()     }      func cleanup() {         self.interactiveCell?.hidden = false         self.interactiveView?.removeFromSuperview()         self.interactiveView = nil         self.interactiveCell = nil         self.interactiveIndexPath = nil         self.previousPoint = nil         self.swapSet.removeAll()     }      func shouldSwap(newPoint: CGPoint) -> Bool {         if let previousPoint = self.previousPoint {             let distance = previousPoint.distanceToPoint(newPoint)             return distance 

I do realize that there is a lot going on there, but I hope everything will be clear in a minute.

  1. Extension on UIView with helper method to get a snapshot of a cell.
  2. Extension on CGPoint with helper method to calculate distance between two points.
  3. SwapDescription helper structure - it is needed to prevent multiple swaps of the same pair of items, which resulted in glitchy animations. Its hashValue method could be improved, but was good enough for the sake of this proof of concept.
  4. beginInteractiveMovementForItemAtIndexPath(indexPath: NSIndexPath) -> Bool - the method called when the movement begins. Everything gets setup here. We get a snapshot of our cell and add it as a subview - this snapshot will be what the user actually drags on screen. The cell itself gets hidden. If you return false from this method, the interactive movement will not begin.
  5. updateInteractiveMovementTargetPosition(targetPosition: CGPoint) - method called after each user movement, which is a lot. We check if the distance from previous point is small enough to swap items - this prevents issue when the user would drag fast across screen and multiple items would get swapped with non-obvious results. If the swap can happen, we check if it is already happening, and if not we swap two items.
  6. endInteractiveMovement(), cancelInteractiveMovement(), cleanup() - after the movement ends, we need to restore our helpers to their default state.
  7. shouldSwap(newPoint: CGPoint) -> Bool - helper method to check if the movement was small enough so we can swap cells.

This is the result it gives :

Let me know if this is what you needed and/or if you need me to clarify something.

Here is a demo project.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!