通过使用手势识别器拖动iOS 9并简化对重新排序的新iOS 9支持,我能够在iOS 9上重新排序。
public func beginInteractiveMovementForItemAtIndexPath(indexPath: NSIndexPath) -> Bool // returns NO if reordering was prevented from beginning - otherwise YES
public func updateInteractiveMovementTargetPosition(targetPosition: CGPoint)
public func endInteractiveMovement()
public func cancelInteractiveMovement()我注意到当我开始拖动一个细胞时,它的中心变成了触控位置,我不喜欢这样。
如果我想的话,我想把我的手机拖到它的角落。
你知道怎么做吗?
非常感谢。
发布于 2017-08-09 20:33:49
(用Swift 3.1编写)
对于targetPosition函数的updateInteractiveMovementTargetPosition参数,而不是像这样直接使用手势识别器的位置.
var location = recognizer.location(in: collectionView)
collectionView.updateInteractiveMovementTargetPosition(location)..。我创建了一个函数,该函数以要拖动的单元格为中心( collectionView updateInteractiveMovementTargetPosition 将使用的位置),然后获取手势识别器的触摸在单元格中的位置,并将其从单元格的中心减去。
func offsetOfTouchFrom(recognizer: UIGestureRecognizer, inCell cell: UICollectionViewCell) -> CGPoint {
let locationOfTouchInCell = recognizer.location(in: cell)
let cellCenterX = cell.frame.width / 2
let cellCenterY = cell.frame.height / 2
let cellCenter = CGPoint(x: cellCenterX, y: cellCenterY)
var offSetPoint = CGPoint.zero
offSetPoint.y = cellCenter.y - locationOfTouchInCell.y
offSetPoint.x = cellCenter.x - locationOfTouchInCell.x
return offSetPoint
}我的视图控制器中有一个简单的var offsetForCollectionViewCellBeingMoved: CGPoint = .zero,它将存储偏移量,所以当手势识别器的位置发生变化时,不需要每次调用上面的函数。
所以我手势识别器的目标是这样的:
func collectionViewLongPressGestureRecognizerWasTriggered(recognizer: UILongPressGestureRecognizer) {
guard let indexPath = collectionView.indexPathForItem(at: recognizer.location(in: self.collectionView)),
let cell = collectionView.cellForItem(at: indexPath), indexPath.item != 0 else { return }
switch recognizer.state {
case .began:
collectionView.beginInteractiveMovementForItem(at: indexPath)
// This is the class variable I mentioned above
offsetForCollectionViewCellBeingMoved = offsetOfTouchFrom(recognizer: recognizer, inCell: cell)
// This is the vanilla location of the touch that alone would make the cell's center snap to your touch location
var location = recognizer.location(in: collectionView)
/* These two lines add the offset calculated a couple lines up to
the normal location to make it so you can drag from any part of the
cell and have it stay where your finger is. */
location.x += offsetForCollectionViewCellBeingMoved.x
location.y += offsetForCollectionViewCellBeingMoved.y
collectionView.updateInteractiveMovementTargetPosition(location)
case .changed:
var location = recognizer.location(in: collectionView)
location.x += offsetForCollectionViewCellBeingMoved.x
location.y += offsetForCollectionViewCellBeingMoved.y
collectionView.updateInteractiveMovementTargetPosition(location)
case .ended:
collectionView.endInteractiveMovement()
default:
collectionView.cancelInteractiveMovement()
}
}发布于 2018-05-14 04:55:13
如果您的集合视图只朝一个方向滚动,那么实现这一点的最简单的方法就是简单地锁定没有滚动到硬编码的轴,这意味着您的单元格将只在可以滚动的轴上移动。这是代码,请参阅changed情况..。
@objc func handleLongGesture(gesture: UILongPressGestureRecognizer) {
switch gesture.state {
case .began:
guard let selectedIndexPath = self.collectionView
.indexPathForItem(at: gesture
.location(in: self.collectionView)) else { break }
collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
case .changed:
var gesturePosition = gesture.location(in: gesture.view!)
gesturePosition.x = (self.collectionView.frame.width / 2) - 20
collectionView.updateInteractiveMovementTargetPosition(gesturePosition)
case .ended:
collectionView.endInteractiveMovement()
default:
collectionView.cancelInteractiveMovement()
}
}https://stackoverflow.com/questions/40116282
复制相似问题