实际上我一直在使用LXReorderableCollectionViewFlowLayout,它非常适合您,并且在他们的存储库中也有一个示例。起初我不建议您使用滑动手势,因为它与 UICollectionView 的内部手势处理高度冲突,但如果您确定需要它,您必须实现添加手势的委托方法并解决同时手势,因为它正在执行 LXReordableCollectionViewFlowLayout:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if ([self.panGestureRecognizer isEqual:gestureRecognizer]) {
return (self.selectedItemIndexPath != nil);
}
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if ([self.longPressGestureRecognizer isEqual:gestureRecognizer]) {
return [self.panGestureRecognizer isEqual:otherGestureRecognizer];
}
if ([self.panGestureRecognizer isEqual:gestureRecognizer]) {
return [self.longPressGestureRecognizer isEqual:otherGestureRecognizer];
}
return NO;
}
无论如何,我不建议您重新实现 LXReordableCollectionViewFlowLayout 中所做的事情,并将其用于您的需要。如果您使用 Storyboard,则需要将 UICollectionView 放入 UIViewController 并将默认 UICollectionViewFlowLayout 类更改为 LXReorderableCollectionViewFlowLayout 就像
此外,您的 UICollectionView 委托应符合下一个协议:LXReorderableCollectionViewDataSource(扩展 UICollectionViewDataSource)和 LXReorderableCollectionViewDelegateFlowLayout(扩展 UICollectionViewDelegateFlowLayout)允许您使用您的类。根据您的需要,只需提供下一个方法就足够了:
- (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath willMoveToIndexPath:(NSIndexPath *)toIndexPath {
id from = [items objectAtIndex:fromIndexPath.item];
id to = [items objectAtIndex:toIndexPath.item];
[items replaceObjectAtIndex:fromIndexPath.item withObject:to];
[items replaceObjectAtIndex:toIndexPath.item withObject:from];;
}
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath canMoveToIndexPath:(NSIndexPath *)toIndexPath
{
return YES;
}
要获得您需要的行为,您必须进入 LXReorderableCollectionViewFlowLayout 类并更改 invalidateLayoutIfNecessary 方法,以便它执行另一种更新,而不是以前:
- (void)invalidateLayoutIfNecessary {
NSIndexPath *newIndexPath = [self.collectionView indexPathForItemAtPoint:self.currentView.center];
NSIndexPath *previousIndexPath = self.selectedItemIndexPath;
if ((newIndexPath == nil) || [newIndexPath isEqual:previousIndexPath]) {
return;
}
if ([self.dataSource respondsToSelector:@selector(collectionView:itemAtIndexPath:canMoveToIndexPath:)] &&
![self.dataSource collectionView:self.collectionView itemAtIndexPath:previousIndexPath canMoveToIndexPath:newIndexPath]) {
return;
}
self.selectedItemIndexPath = newIndexPath;
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(self) strongSelf = weakSelf;
if (strongSelf) {
// NOTE: cells swipe
[strongSelf.collectionView moveItemAtIndexPath:previousIndexPath toIndexPath:newIndexPath];
[strongSelf.collectionView moveItemAtIndexPath:newIndexPath toIndexPath:previousIndexPath];
// NOTE: it was here previously
// [strongSelf.collectionView deleteItemsAtIndexPaths:@[ previousIndexPath ]];
// [strongSelf.collectionView insertItemsAtIndexPaths:@[ newIndexPath ]];
}
} completion:^(BOOL finished) {
__strong typeof(self) strongSelf = weakSelf;
if ([strongSelf.dataSource respondsToSelector:@selector(collectionView:itemAtIndexPath:didMoveToIndexPath:)]) {
[strongSelf.dataSource collectionView:strongSelf.collectionView itemAtIndexPath:previousIndexPath didMoveToIndexPath:newIndexPath];
}
}];
}
它适用于靠近您初始项目的单元格,但是一旦您的卡移动,它可能会误导用户下一步将其移动到哪里,无论如何只需尝试一下,看看它是如何进行的。请记住,您可以通过 collectionView:itemAtIndexPath:canMoveToIndexPath: 方法控制项目的可用移动。
希望对您有所帮助。