var location = recognizer.location(in: collectionView)
collectionView.updateInteractiveMovementTargetPosition(location)
が...私は機能を作成した...代わりに、このように直接ジェスチャー認識の場所を使用しての、
updateInteractiveMovementTargetPosition
機能の
targetPosition
パラメータについて
(スウィフト3.1で書かれました) (コレクションビュー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 += offsetForPaletteCellBeingMoved.x
location.y += offsetForPaletteCellBeingMoved.y
collectionView.updateInteractiveMovementTargetPosition(location)
case .changed:
var location = recognizer.location(in: collectionView)
location.x += offsetForPaletteCellBeingMoved.x
location.y += offsetForPaletteCellBeingMoved.y
collectionView.updateInteractiveMovementTargetPosition(location)
case .ended:
collectionView.endInteractiveMovement()
default:
collectionView.cancelInteractiveMovement()
}
}
をあなたが解決策を見つけたことがありますか? – Nico
まだ残念ながら。 – thierryb