gpt4 book ai didi

ios - UICollectionview 重新排序图像单元格

转载 作者:行者123 更新时间:2023-11-30 12:38:14 26 4
gpt4 key购买 nike

我有一个带有 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()
}
}

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

reorder default

我用下面的代码让它在“后面”工作:

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 时,您可以像已经做的那样更改位置。

关于ios - UICollectionview 重新排序图像单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42603533/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com