「LXReorderableCollectionViewFlowLayout」を使って並び替え可能なUICollectionViewを実現する
UICollectionViewで、iPhoneのホーム画面みたいに長押しして並び替え出来ないかなーと探していたら良いライブラリがありました。
lxcid/LXReorderableCollectionViewFlowLayout
https://github.com/lxcid/LXReorderableCollectionViewFlowLayout

インストール
CocoaPodsから。
pod 'LXReorderableCollectionViewFlowLayout'
実装
Storyboard
Collection View Controllerを設置し、CollectionViewFlowLayoutSize に LXReorderbleCollectionViewFlowLayout を設定する。
実装先のUICollectionViewController
UICollectionViewControllerのサブクラスに protocol を設定
@interface EXCollectionController : UICollectionViewController <LXReorderableCollectionViewDataSource, LXReorderableCollectionViewDelegateFlowLayout>
- デリゲートメソッドの実装
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)theCollectionView numberOfItemsInSection:(NSInteger)theSectionIndex
{
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor greenColor];
return cell;
}
#pragma mark - LXReorderableCollectionViewDataSource
- (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath willMoveToIndexPath:(NSIndexPath *)toIndexPath
{
}
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath canMoveToIndexPath:(NSIndexPath *)toIndexPath
{
return YES;
}
#pragma mark - LXReorderableCollectionViewDelegateFlowLayout methods
- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout willBeginDraggingItemAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout didBeginDraggingItemAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout willEndDraggingItemAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout didEndDraggingItemAtIndexPath:(NSIndexPath *)indexPath
{
}
ちなみに
iPhoneアプリ「フィギュコレ」でも使われているようです。
【ニコニコ動画】CollectionViewをiPhoneホーム画面風に改造してみた


