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

标签 ios objective-c uicollectionview

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

我做了 project在 Github 上公开,如果您想添加为合作者,请告诉我?

我正在对 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;
    }
}

你知道如何保持尺寸吗?

最佳答案

我遇到了同样的问题,原因是你需要调用委托(delegate)的 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;
}

关于ios - UICollectionView 移动项目并保持单元格大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39977555/

相关文章:

ios - 将 AVCaptureVideoPreviewLayer 输出裁剪为正方形

ios - LaunchScreen.xib 工具栏中 UIBarButtonItem 的自定义系统字体

ios - 从 appdelegate 转到 View Controller

iphone - 添加导航栏按钮的最佳位置?

javascript - 如何修复您应该只在 IOS 的应用程序中显式渲染一个导航器

objective-c - NSScrollView 缩放故障

ios - 使用自动布局调整 super View 和所有 super View 的 sibling

ios - UICollectionView - 当键盘出现时,整个 Collection View 随键盘移动

ios - 滚动到 UICollectionView 中的下一个单元格

ios - Swift - 检索数据后显示 UICollectionView