2016-10-18 4 views
1

제스처 인식기를 사용하여 드래그하고 재정렬에 대한 iOS 9 지원을 새로 추가하여 iOS 9에서 내 UICollectionViewCell을 재정렬 할 수 있습니다.드래그 할 때 가운데로 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 파라미터 들어

(SWIFT 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() 
    } 
} 
관련 문제