我得到了一个UICollectionview
的XX图像/单元格,我希望在拖放时让它们交换位置。我正在使用内置重新排序功能,但它无法满足我的需求。这是代码:
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
}
答案 0 :(得分:0)
当您识别出对单元格的长按时,您可以创建collectionViewCell的contentView的副本。然后,您可以使用触摸(touchesMoved)移动该副本,同时所有单元格仍处于静止状态,当用户放下它时(touchesEnded),您可以计算该位置的indexPath。当你拥有indexPath时,你可以改变你已经在做的位置。