我尝试根据本指南实现从iOS10开始提供的内置CollectionView重新排序:http://nshint.io/blog/2015/07/16/uicollectionviews-now-have-easy-reordering/ 在iOS11上一切正常,但在UICollectionView中开始拖动任何项目(单元格)时,iOS10上会崩溃
这是我的代码:
- (void)addCollectionViewGestureRecognizers
{
if (!self.panGesture)
{
// gesture to drag and reorder tiles
self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleCollectionViewPanGesture:)];
[self.collectionView addGestureRecognizer:self.panGesture];
}
}
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
CLog(@"Item moved from index path: %@ to index path: %@", sourceIndexPath, destinationIndexPath);
}
- (void)handleCollectionViewPanGesture:(UIPanGestureRecognizer *)gesture
{
switch(gesture.state)
{
case UIGestureRecognizerStateBegan:
{
NSIndexPath *selectedIndexPath = [self.collectionView indexPathForItemAtPoint:[gesture locationInView:self.collectionView]];
if(selectedIndexPath)
{
[self.collectionView beginInteractiveMovementForItemAtIndexPath:selectedIndexPath];
}
break;
}
case UIGestureRecognizerStateChanged:
{
[self.collectionView updateInteractiveMovementTargetPosition:[gesture locationInView:gesture.view]];
break;
}
case UIGestureRecognizerStateEnded:
{
[self.collectionView endInteractiveMovement];
break;
}
default:
{
[self.collectionView cancelInteractiveMovement];
break;
}
}
}
和iOS 10每次都在线崩溃:
[self.collectionView updateInteractiveMovementTargetPosition:[gesture locationInView:guesture.view]];
崩溃报告:
0 CoreFoundation 0x0000000118f8bb0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000118961141 objc_exception_throw + 48
2 CoreFoundation 0x0000000118ec0443 -[__NSArrayM insertObject:atIndex:] + 1603
3 UIKit 0x00000001166f3577 -[UICollectionView _getOriginalReorderingIndexPaths:targetIndexPaths:] + 501
4 UIKit 0x00000001166e2e50 -[UICollectionView _viewAnimationsForCurrentUpdate] + 7338
5 UIKit 0x00000001166e7bf3 __71-[UICollectionView _updateWithItems:tentativelyForReordering:animator:]_block_invoke.2012 + 197
6 UIKit 0x0000000115e4c08e +[UIView(Animation) performWithoutAnimation:] + 90
7 UIKit 0x00000001166e682d -[UICollectionView _updateWithItems:tentativelyForReordering:animator:] + 3856
8 UIKit 0x00000001166e0b33 -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:] + 17030
9 UIKit 0x00000001166e91cd -[UICollectionView _endUpdatesWithInvalidationContext:tentativelyForReordering:animator:] + 71
10 UIKit 0x00000001166e9514 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:animator:] + 437
11 UIKit 0x00000001166e933c -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:] + 91
12 UIKit 0x00000001166f09a3 -[UICollectionView _updateReorderingTargetPosition:forced:] + 1467
13 UIKit 0x00000001166f122a -[UICollectionView updateInteractiveMovementTargetPosition:] + 29
14 myAPP 0x000000010dcb3b26 -[MenuCollectionViewController handleCollectionViewPanGesture:] + 710
15 UIKit 0x00000001162c8f59 -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 57
16 UIKit 0x00000001162d0d57 _UIGestureRecognizerSendTargetActions + 109
17 UIKit 0x00000001162ce70b _UIGestureRecognizerSendActions + 225
我忘记了什么?
如果有人对此有任何想法,我将非常感激......
答案 0 :(得分:0)
好吧,好像我找到了崩溃和可能的解决方案的原因。 我有从UICollectionViewFlowLayout继承的自定义布局,并覆盖其中的函数targetIndexPathForInteractivelyMovingItem。将项目移动到正确的位置。 我这样实现了它:
- (NSIndexPath*)targetIndexPathForInteractivelyMovingItem:(NSIndexPath *)previousIndexPath withPosition:(CGPoint)position
{
NSIndexPath *targetIndexPath = [self.collectionView indexPathForItemAtPoint:position];
return targetIndexPath;
}
这在iOS 11上工作正常,但对于iOS 10,indexPathForItemAtPoint由于某种原因返回nil(我不确定为什么) 所以我不得不添加这个技巧,以防止重新排序崩溃
- (NSIndexPath*)targetIndexPathForInteractivelyMovingItem:(NSIndexPath *)previousIndexPath withPosition:(CGPoint)position
{
NSIndexPath *targetIndexPath = [self.collectionView indexPathForItemAtPoint:position];
if (!targetIndexPath) // on iOS10 indexpath can be nil by some reason
{
targetIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
}
return targetIndexPath;
}