我有不同大小的单元格,我正在尝试移动 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/