UICollectionView移动项目并保持单元格大小

时间:2016-10-11 12:44:36

标签: ios objective-c uicollectionview

我有不同大小的单元格,我正在尝试移动CollectionView单元格,并保持移动/选定的单元格大小移动到新位置。

我已经在Github上公开了project,如果你想加入合作者,请告诉我?

我正在对UICollectionViewFlowLayout进行子分类,并实施了以下内容:

- (UICollectionViewLayoutInvalidationContext *)invalidationContextForInteractivelyMovingItems:(NSArray<NSIndexPath *> *)targetIndexPaths withTargetPosition:(CGPoint)targetPosition previousIndexPaths:(NSArray<NSIndexPath *> *)previousIndexPaths previousPosition:(CGPoint)previousPosition{

    UICollectionViewLayoutInvalidationContext *context = [super
                                                          invalidationContextForInteractivelyMovingItems:targetIndexPaths
                                                          withTargetPosition:targetPosition
                                                          previousIndexPaths:previousIndexPaths
                                                          previousPosition:previousPosition];

    [self.collectionView moveItemAtIndexPath:previousIndexPaths[0] toIndexPath:targetIndexPaths[0]];

    return context;
}

还试图在UICollectionViewController中实现它:

-(NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveFromItemAtIndexPath:(NSIndexPath *)originalIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath{
    if (originalIndexPath.section == proposedIndexPath.section) {
        return  proposedIndexPath;
    }else{
        return originalIndexPath;
    }
}

你知道怎么保持这个尺寸吗?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,原因是你需要调用委托的moveItemAtIndexPath。

- (UICollectionViewLayoutInvalidationContext *)invalidationContextForInteractivelyMovingItems:(NSArray<NSIndexPath *> *)targetIndexPaths withTargetPosition:(CGPoint)targetPosition previousIndexPaths:(NSArray<NSIndexPath *> *)previousIndexPaths previousPosition:(CGPoint)previousPosition{

    UICollectionViewLayoutInvalidationContext *context = [super
                                                          invalidationContextForInteractivelyMovingItems:targetIndexPaths
                                                          withTargetPosition:targetPosition
                                                          previousIndexPaths:previousIndexPaths
                                                          previousPosition:previousPosition];

    [self.collectionView.dataSource collectionView:self.collectionView moveItemAtIndexPath:previousIndexPaths[0] toIndexPath:targetIndexPaths[0]];

    return context;
}