给cell长按手势 创建截图 然后拖拽截图和cell交互判断是否需要改变秩序
//移动所需参数
@property(nonatomic,strong)UIView *snapShotView;
@property(nonatomic,strong)NSIndexPath *moveIndexPath;
@property(nonatomic,strong)UICollectionViewCell *moveCell;
这里最好cell的代理方法 返回手势
//长按手势
UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(gestureAction:)];
//longGes.delegate = self;
[cell addGestureRecognizer:longGes];
- (void)gestureAction:(UITapGestureRecognizer *)gestureRecognizer
{
//记录上一次的手势的位置
static CGPoint startPoint;
//cell
UICollectionViewCell *orignCell = (UICollectionViewCell *)gestureRecognizer.view;
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
NSLog(@"开始-长按手势");
}
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
NSLog(@"开始-pan手势");
}
//获取截图
self.snapShotView = [orignCell snapshotViewAfterScreenUpdates:YES];
self.snapShotView.center = orignCell.center;
[self.mainView addSubview:self.snapShotView];
orignCell.hidden = YES;
self.moveIndexPath = [self.mainView indexPathForCell:orignCell];
self.moveCell = orignCell;
startPoint = [gestureRecognizer locationInView:self.mainView];
} else if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {
//NSLog(@"change");
CGFloat tranX = [gestureRecognizer locationOfTouch:0 inView:self.mainView].x - startPoint.x;
CGFloat tranY = [gestureRecognizer locationOfTouch:0 inView:self.mainView].y - startPoint.y;
//设置截图位置
self.snapShotView.center = CGPointApplyAffineTransform(self.snapShotView.center, CGAffineTransformMakeTranslation(tranX, tranY));
startPoint = [gestureRecognizer locationOfTouch:0 inView:self.mainView];
//计算交互
for (UICollectionViewCell *itemCell in [self.mainView visibleCells]) {
//跳过隐藏的cell 和固定cell 如添加cell
if ([self.mainView indexPathForCell:itemCell] == self.moveIndexPath) {
continue;
}
//计算中心距
//powf/pow(double x, double y);计算以x为底数的y次幂
//sqrt() / sqrtf() / sqrtl() ----求平方根
CGFloat space = sqrtf(pow(self.snapShotView.center.x - itemCell.center.x, 2) + powf(self.snapShotView.center.y - itemCell.center.y, 2));
//如果相交一半且两个视图Y的绝对值小于高度的一半就移动
// fabs (double);求绝对值
if (fabs(self.snapShotView.center.y - itemCell.center.y) <= self.snapShotView.height*0.5 && space <= self.snapShotView.bounds.size.width*0.5)
{
//将要移动到的位置
NSIndexPath *endIndexPath = [self.mainView indexPathForCell:itemCell];
//改变数据源
//如果起始startCell滑动到后面endCell 把起始startCell值保存起来 然后把endCell前面和startCellz后面的cell 整体向前移动一位 包括endCell需要向前移动
if (self.moveIndexPath.row < endIndexPath.row) {
//向后移动
//1.moveIndexPath之后 包括moveIndexPath 所有数据都获取下一个数据
//2.endIndexPath 截止 不包括endIndexPath
//3.endIndexPath 值是moveIndexPath原来的值
} else {
//向前移动
//1.endIndexPath 之后数据开始 不包括endIndexPath 那么起始位置就是endIndexPath的下一个 所有数据都是上一个数据
//2.moveIndexPath 截止 包括moveIndexPath
//3.endIndexPath 值是moveIndexPath原来的值
}
//UI改变
[self.mainView moveItemAtIndexPath:self.moveIndexPath toIndexPath:endIndexPath];
self.moveIndexPath = endIndexPath;
break;
}
}
} else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
NSLog(@"结束");
self.moveCell.hidden = NO;
[self.snapShotView removeFromSuperview];
self.snapShotView = nil;
}
}
切换数据核心代码
if (self.moveIndexPath.row < endIndexPath.row) {
//向后移动
//1.moveIndexPath之后 包括moveIndexPath 所有数据都获取下一个数据
//2.endIndexPath 截止 不包括endIndexPath
//3.endIndexPath 值是moveIndexPath原来的值
//PushPhotoItemModel *moveItemModel = self.dataSource[self.moveIndexPath.row];
for (NSInteger index = self.moveIndexPath.row; index < endIndexPath.row; index ++) {
[self.dataSource replaceObjectAtIndex:index withObject:self.dataSource[index + 1]];
}
//[self.dataSource replaceObjectAtIndex:endIndexPath.row withObject:moveItemModel];
} else {
//向前移动
//1.endIndexPath 之后数据开始 不包括endIndexPath 那么起始位置就是endIndexPath的下一个 所有数据都是上一个数据
//2.moveIndexPath 截止 包括moveIndexPath
//3.endIndexPath 值是moveIndexPath原来的值
//PushPhotoItemModel *moveItemModel = self.dataSource[self.moveIndexPath.row];
//这样数据就被改动了
for (NSInteger index = (endIndexPath.row + 1); index <= self.moveIndexPath.row; index ++) {
//[self.dataSource replaceObjectAtIndex:index withObject:self.dataSource[index - 1]];
}
for (NSInteger index = self.moveIndexPath.row; index > endIndexPath.row; index --) {
[self.dataSource replaceObjectAtIndex:index withObject:self.dataSource[index - 1]];
}
//[self.dataSource replaceObjectAtIndex:endIndexPath.row withObject:moveItemModel];
}
网友评论