繁体   English   中英

UICollectionview重新排序图像单元格

[英]UICollectionview reorder image cells

我有一个具有XX个图像/单元格的UICollectionview ,我想让它们在拖放时交换位置。 我正在使用内置的重新排序功能,但无法满足我的需求。 这是代码:

longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongGesture))
        longPressGesture.minimumPressDuration = 0.2
        longPressGesture.delaysTouchesBegan = true

        cameraRollCollectionView.addGestureRecognizer(longPressGesture)

func handleLongGesture(gesture: UILongPressGestureRecognizer) {

        switch(gesture.state) {

        case UIGestureRecognizerState.began:
            guard let selectedIndexPath = cameraRollCollectionView.indexPathForItem(at: gesture.location(in: cameraRollCollectionView)) else {
                break
            }
            cameraRollCollectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
        case UIGestureRecognizerState.changed:
            cameraRollCollectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
        case UIGestureRecognizerState.ended:
            cameraRollCollectionView.endInteractiveMovement()
        default:
            cameraRollCollectionView.cancelInteractiveMovement()
        }
    }

我基本上只希望动画只交换移动单元格和目标单元格,而不像附加图像那样对“邻居”进行重新排序。

重新订购默认

我使用以下代码在“后面”工作:

func collectionView(_ collectionView: UICollectionView,
                        moveItemAt sourceIndexPath: IndexPath,
                        to destinationIndexPath: IndexPath) {


        let temp = selectedUIImages[sourceIndexPath.row]
        selectedUIImages[sourceIndexPath.row] = selectedUIImages[destinationIndexPath.row]
        selectedUIImages[destinationIndexPath.row] = temp
 }

当您认识到长按单元格时,可以创建collectionViewCell的contentView的副本。 然后,您可以在所有单元格都静止时通过触摸(touchesMoved)移动该副本,并且当用户放下它(touchesEnded)时,可以计算该位置的indexPath。 拥有indexPath时,可以像已经做的那样更改位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM