实现UICollection的重新排列功能的常规方法是执行以下操作:
内部,比方说,viewDidLoad:
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress(gesture:)))
longPress.minimumPressDuration = 0.2
self.collectionView.addGestureRecognizer(longPress)
然后是handleLongPress函数:
func handleLongPress(gesture: UILongPressGestureRecognizer){
switch(gesture.state){
case .began:
guard let selected = self.collectionView.indexPathForItem(at: gesture.location(in: self.collectionView)) else{
break
}
self.collectionView.beginInteractiveMovementForItem(at: selected)
case .changed:
self.collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
case .ended:
self.collectionView.endInteractiveMovement()
default:
self.collectionView.cancelInteractiveMovement()
}
}
并实施
func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool { ... }
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { ... }
这使得我可以长时间按下单元格的任何位置。然而,当我长按最右边或最左边时,细胞集中在我触摸的位置。我可以禁用居中功能吗?
答案 0 :(得分:0)
case .changed:
var location:CGPoint = gesture.location(in: gesture.view!)
location.x = self.collectionView.frame.midX
self.collectionView.updateInteractiveMovementTargetPosition(location)
我最终需要