lxcid / LXReorderableCollectionViewFlowLayout

Extends `UICollectionViewFlowLayout` to support reordering of cells. Similar to long press and pan on books in iBook.

Home Page:http://lxcid.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

proper method names and usage for layout

viking2009 opened this issue · comments

add optional method to LXReorderableCollectionViewDataSource

- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;

check if animation finished, perform moving in performBatchUpdates

    if ([self.dataSource respondsToSelector:@selector(collectionView:itemAtIndexPath:willMoveToIndexPath:)]) {
        [self.dataSource collectionView:self.collectionView itemAtIndexPath:previousIndexPath willMoveToIndexPath:newIndexPath];
    }

    __weak typeof(self)weakSelf = self;
    [self.collectionView performBatchUpdates:^{
        __strong typeof(weakSelf)strongSelf = weakSelf;
        if (strongSelf) {
            if ([strongSelf.dataSource respondsToSelector:@selector(collectionView:moveItemAtIndexPath:toIndexPath:)]) {
                [strongSelf.dataSource collectionView:strongSelf.collectionView moveItemAtIndexPath:previousIndexPath toIndexPath:newIndexPath];
            }

            [strongSelf.collectionView deleteItemsAtIndexPaths:@[ previousIndexPath ]];
            [strongSelf.collectionView insertItemsAtIndexPaths:@[ newIndexPath ]];
        }
    } completion:^(BOOL finished) {
        if (finished) {
                __strong typeof(weakSelf)strongSelf = weakSelf;
                if ([strongSelf.dataSource respondsToSelector:@selector(collectionView:itemAtIndexPath:didMoveToIndexPath:)]) {
                    [strongSelf.dataSource collectionView:strongSelf.collectionView itemAtIndexPath:previousIndexPath didMoveToIndexPath:newIndexPath];
                }
        }
    }];