UICollectionView effective drag and drop

前端 未结 5 1928
半阙折子戏
半阙折子戏 2021-01-29 17:56

I am currently trying to implement the UITableView reordering behavior using UICollectionView.

Let\'s call a UItableView TV and a UICollectionVi

相关标签:
5条回答
  • 2021-01-29 18:32

    If you want to experiment rolling out your own, I just wrote a Swift based tutorial you can look. I tried to build the most basic of cases so as to be easier to follow this.

    0 讨论(0)
  • 2021-01-29 18:36

    This might help

    https://github.com/lxcid/LXReorderableCollectionViewFlowLayout

    This is extends the UICollectionView to allow each of the UICollectionViewCells to be rearranged manually by the user with a long touch (aka touch-and-hold). The user can drag the Cell to any other position in the collection and the other cells will reorder automatically. Thanks go to lxcid for this.

    0 讨论(0)
  • 2021-01-29 18:43

    Here is an alternative:

    The differences between DraggableCollectionView and LXReorderableCollectionViewFlowLayout are:

    • The data source is only changed once. This means that while the user is dragging an item the cells are re-positioned without modifying the data source.
    • It's written in such a way that makes it possible to use with custom layouts.
    • It uses a CADisplayLink for smooth scrolling and animation.
    • Animations are canceled less frequently while dragging. It feels more "natural".
    • The protocol extends UICollectionViewDataSource with methods similar to UITableViewDataSource.

    It's a work in progress. Multiple sections are now supported.

    To use it with a custom layout see DraggableCollectionViewFlowLayout. Most of the logic exists in LSCollectionViewLayoutHelper. There is also an example in CircleLayoutDemo showing how to make Apple's CircleLayout example from WWDC 2012 work.

    0 讨论(0)
  • 2021-01-29 18:55

    Here is another approach:

    Key difference is that this solution does not require a "ghost" or "dummy" cell to provide the drag and drop functionality. It simply uses the cell itself. Animations are in line with UITableView. It works by adjusting the collection view layout's private datasource while moving around. Once you let go, it will tell your controller that you can commit the change to your own datasource.

    I believe it's a bit simpler to work with for most use cases. Still a work in progress, but yet another way to accomplish this. Most should find this pretty easy to incorporate into their own custom UICollectionViewLayouts.

    0 讨论(0)
  • 2021-01-29 18:57

    As of iOS 9, UICollectionView now supports reordering.

    For UICollectionViewControllers, just override collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)

    For UICollectionViews, you'll have to handle the gestures yourself in addition to implementing the UICollectionViewDataSource method above.

    Here's the code from the source:

    private var longPressGesture: UILongPressGestureRecognizer!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        longPressGesture = UILongPressGestureRecognizer(target: self, action: "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()
        }
    }
    

    Sources: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/#//apple_ref/doc/uid/TP40012177-CH1-SW67

    http://nshint.io/blog/2015/07/16/uicollectionviews-now-have-easy-reordering/

    0 讨论(0)
提交回复
热议问题