我有不同大小的单元格,我正在尝试移动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;
}
}
你知道如何保持尺寸吗?
最佳答案
我遇到了同样的问题,原因是您需要调用委托的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/