
相比 UITableView,UICollectionView 擁有更高的客製彈性。iOS 9 之後,Collection View 支援更簡單的項目重排,讓開發者不必再完全手刻拖曳邏輯。
最簡單的方式是使用 UICollectionViewController。它提供 installsStandardGestureForInteractiveMovement 屬性,預設為開啟,只要實作 moveItemAtIndexPath 方法,即可讓 cell 重新排序。

若使用一般 UIViewController,则需要自行加入長按手勢,並依手勢狀態呼叫 beginInteractiveMovementForItemAtIndexPath、updateInteractiveMovementTargetPosition、endInteractiveMovement 或 cancelInteractiveMovement。

互動移動也能套用在自訂 layout 上,例如瀑布流。若希望移動中的 cell 保持原本大小,可透過 UICollectionViewLayout 的 invalidation context 方法介入處理。


整體而言,這套 API 讓 Collection View 的排序互動變得直觀,也更容易與自訂版面配置整合。