//实现item的重排
- (void)longPressGestureRecognizerHandle:(UILongPressGestureRecognizer *)longPressGestureRecognizer
{
CGPoint point = [longPressGestureRecognizer locationInView:self.collectionView];
if (longPressGestureRecognizer.state == UIGestureRecognizerStateBegan)
{
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
//item开始交互移动(iOS9.0新特新)
[self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
}
else if (longPressGestureRecognizer.state == UIGestureRecognizerStateChanged)
{
//更新交互移动坐标
[self.collectionView updateInteractiveMovementTargetPosition:point];
}
else if (longPressGestureRecognizer.state == UIGestureRecognizerStateEnded)
{
//结束交互移动
[self.collectionView endInteractiveMovement];
}
else
{
//取消交互移动
[self.collectionView cancelInteractiveMovement];
}
}
#pragma mark - UICollectionViewDataSource
/**
* 实现item的移动
*
* @param collectionView
* @param sourceIndexPath 开始移动的位置
* @param destinationIndexPath 移动后的位置
*/
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//1.记录要移动的数据
id o = self.dataArray[sourceIndexPath.item];
//2.删除要移动的数据
[self.dataArray removeObject:o];
//3.添加要移动的数据到指定位置
[self.dataArray insertObject:o atIndex:destinationIndexPath.item];
}