2016-10-18 3 views
1

iOS 9で私のUICollectionViewCellsをジェスチャーレコグナイザを使ってドラッグして並べ替えることができます。ドラッグするときに中央でUICollectionViewCellを移動させないようにする

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() 

私はタッチ位置であることを、その中心が変化するセルのドラッグを開始するとき、私はそれを好きではないことに気づきました。

私が望むなら、私はそれをコーナーでドラッグすることができます。

あなたはこれを行う方法を知っていますか?

ありがとうございます。

+0

をあなたが解決策を見つけたことがありますか? – Nico

+0

まだ残念ながら。 – thierryb

答えて

3

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() 
    } 
} 
関連する問題