UICollectionView有效拖放

foO*_*oOg 59 scroll drag-and-drop uiscrollview uicollectionview

我目前正在尝试使用UICollectionView实现UITableView重新排序行为.

让我们调用一个UItableView电视和一个UICollectionView CV(以澄清以下解释)

我基本上试图重现电视的拖放,但我没有使用编辑模式,一旦触发长按手势,单元就可以移动了.它完美地工作,我使用CV的移动方法,一切都很好.

我更新CV的contentOffset属性以在用户拖动单元格时处理滚动.当用户转到顶部和底部的特定矩形时,我更新了contentOffset和CV滚动.问题是当用户停止移动手指时,手势不会发送任何更新,这会使滚动停止并在用户移动手指后立即重新开始.

这种行为绝对不自然,我宁愿继续滚动,直到用户发布CV,就像电视中的情况一样.电视拖放体验很棒,我真的想重现同样的感觉.有没有人知道他们在重新排序时如何管理电视中的滚动?

  • 我尝试使用计时器重复触发滚动动作,只要手势位置在正确的位置,滚动很糟糕且效率不高(非常慢和跳跃).
  • 我也尝试使用GCD在另一个线程中监听手势位置,但结果甚至更糟.

我对此没有任何想法,所以如果有人有答案,我会嫁给他!

以下是longPress方法的实现:

- (void)handleLongPress:(UILongPressGestureRecognizer *)sender
{
    ReorganizableCVCLayout *layout = (ReorganizableCVCLayout *)self.collectionView.collectionViewLayout;
    CGPoint gesturePosition = [sender locationInView:self.collectionView];
    NSIndexPath *selectedIndexPath = [self.collectionView indexPathForItemAtPoint:gesturePosition];

    if (sender.state == UIGestureRecognizerStateBegan)
    {
        layout.selectedItem = selectedIndexPath;
        layout.gesturePoint = gesturePosition; // Setting gesturePoint invalidate layout
    }
    else if (sender.state == UIGestureRecognizerStateChanged)
    {
        layout.gesturePoint = gesturePosition; // Setting gesturePoint invalidate layout
        [self swapCellAtPoint:gesturePosition];
        [self manageScrollWithReferencePoint:gesturePosition];
    }
    else
    {
        [self.collectionView performBatchUpdates:^
        {
            layout.selectedItem = nil;
            layout.gesturePoint = CGPointZero; // Setting gesturePoint invalidate layout
        } completion:^(BOOL completion){[self.collectionView reloadData];}];
    }
}
Run Code Online (Sandbox Code Playgroud)

要使CV滚动,我使用的方法:

- (void)manageScrollWithReferencePoint:(CGPoint)gesturePoint
{
    ReorganizableCVCLayout *layout = (ReorganizableCVCLayout *)self.collectionView.collectionViewLayout;
    CGFloat topScrollLimit = self.collectionView.contentOffset.y+layout.itemSize.height/2+SCROLL_BORDER;
    CGFloat bottomScrollLimit = self.collectionView.contentOffset.y+self.collectionView.frame.size.height-layout.itemSize.height/2-SCROLL_BORDER;
    CGPoint contentOffset = self.collectionView.contentOffset;

    if (gesturePoint.y < topScrollLimit && gesturePoint.y - layout.itemSize.height/2 - SCROLL_BORDER > 0)
        contentOffset.y -= SCROLL_STEP;
    else if (gesturePoint.y > bottomScrollLimit &&
             gesturePoint.y + layout.itemSize.height/2 + SCROLL_BORDER < self.collectionView.contentSize.height)
        contentOffset.y += SCROLL_STEP;

    [self.collectionView setContentOffset:contentOffset];
}
Run Code Online (Sandbox Code Playgroud)

小智 70

这可能有所帮助

https://github.com/lxcid/LXReorderableCollectionViewFlowLayout

这扩展UICollectionView到允许UICollectionViewCells用户通过长触摸(也称为触摸并保持)手动重新布置每个.用户可以将Cell拖动到集合中的任何其他位置,其他单元格将自动重新排序.谢谢你去lxcid吧.

  • 不鼓励发布仅仅是链接的答案.总结这篇文章的内容将大大改善这个答案. (20认同)
  • 当细胞大小不同时如何拖动细胞? (2认同)

Luk*_*uke 47

是一个替代方案:

DraggableCollectionView和LXReorderableCollectionViewFlowLayout之间的区别是:

  • 数据源仅更改一次.这意味着当用户拖动项目时,将重新定位单元格而不修改数据源.
  • 它的编写方式使得可以使用自定义布局.
  • 它使用CADisplayLink平滑滚动和动画.
  • 在拖动时,动画会被取消.感觉更"自然".
  • 该协议扩展UICollectionViewDataSource与类似的方法UITableViewDataSource.

这是一项正在进行中的工作.现在支持多个部分.

要将其与自定义布局一起使用,请参阅DraggableCollectionViewFlowLayout.大多数逻辑存在于LSCollectionViewLayoutHelper.CircleLayoutDemo中还有一个示例,展示了如何从WWDC 2012开始制作Apple的CircleLayout示例.


chr*_*nse 30

从iOS 9开始,UICollectionView现在支持重新排序.

对于UICollectionViewControllers,只需覆盖collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)

对于UICollectionViews,除了实现上述UICollectionViewDataSource方法之外,您还必须自己处理手势.

这是源代码:

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()
    }
}
Run Code Online (Sandbox Code Playgroud)

来源: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/

  • 我无法让它工作,是否有必须更改的设置或其他任何东西? (2认同)