Vấn đề ở đây là bạn đặt UICollectionView bên trong UIContainerView đang nằm bên trong UIViewController. Điều này chỉ đòi hỏi một vài bước nữa để UICollectionView hoạt động như mong đợi.
Thêm dòng sau vào viewDidLoad trong CollectionViewController của bạn:
self.collectionView!.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: "handleLongGesture:"))
Sau đó, thêm chức năng sau đây để CollectionViewController của bạn:
func handleLongGesture(gesture: UILongPressGestureRecognizer)
{
switch(gesture.state)
{
case UIGestureRecognizerState.Began:
guard let selectedIndexPath = self.collectionView!.indexPathForItemAtPoint(gesture.locationInView(self.collectionView)) else
{
break
}
collectionView!.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath)
case UIGestureRecognizerState.Changed:
collectionView!.updateInteractiveMovementTargetPosition(gesture.locationInView(gesture.view!))
case UIGestureRecognizerState.Ended:
collectionView!.endInteractiveMovement()
default:
collectionView!.cancelInteractiveMovement()
}
}
Cuối cùng chỉ cần đảm bảo để bao gồm những điều sau đây để đảm bảo bạn xử lý các nguồn dữ liệu chính xác:
override func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath,toIndexPath destinationIndexPath: NSIndexPath) {
// Swap the values of the source and destination
}
Kiểm tra điều này link để biết thêm về điều này.
Hy vọng điều này sẽ giúp bạn.
Cập nhật ví dụ với giải pháp bởi @Scooter [ở đây] (https://github.com/jameszaghini/UICollectionViewController-reordering-problem/tree/fixed) –