效果
实现代码
添加手势
UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
[self.view addGestureRecognizer:longPress];
手势响应
- (void)longPressAction:(UIGestureRecognizer*)gesture
{
UIGestureRecognizerState state = gesture.state;
CGPoint location = [gesture locationInView:self.collectionView];
NSIndexPath *currentPath = [self.collectionView indexPathForItemAtPoint:location];
static NSInteger fromIndex = 0;
static NSIndexPath *oriIndexpath = nil;
static UIView *snapShotView = nil;
switch (state) {
case UIGestureRecognizerStateBegan:
{
oriIndexpath = currentPath;
fromIndex = currentPath.row;
TPEditColumnCell *oriCell = (TPEditColumnCell*)[self.collectionView cellForItemAtIndexPath:oriIndexpath];
snapShotView = [self customSnapshoFromView:oriCell];
snapShotView.backgroundColor = [UIColor clearColor];
__block CGPoint center = oriCell.center;
snapShotView.center = center;
snapShotView.alpha = 0.0;
[self.collectionView addSubview:snapShotView];
[UIView animateWithDuration:0.35 animations:^{
center.x = location.x;
center.y = location.y;
snapShotView.center = center;
snapShotView.transform = CGAffineTransformMakeScale(1.1, 1.1);
snapShotView.alpha = 0.9;
oriCell.hidden = YES;
oriCell.alpha = 0.0f;
}];
break;
}
case UIGestureRecognizerStateChanged:
{
if (!snapShotView) {
return;
}
CGPoint snapShotCenter = snapShotView.center;
snapShotCenter.x = location.x;
snapShotCenter.y = location.y;
snapShotView.center = snapShotCenter;
UICollectionViewCell *currentCell = [self.collectionView cellForItemAtIndexPath:currentPath];
UICollectionViewCell *oriCell = [self.collectionView cellForItemAtIndexPath:oriIndexpath];
currentCell.alpha = 0.0f;
oriCell.alpha = 0.0f;
if (currentPath.row == 0 || currentPath.section == 1) {
oriCell.alpha= 1.0f;
currentCell.alpha = 1.0f;
break;
}
if (currentPath &&![currentPath isEqual:oriIndexpath]) {
TPSectionViewModel *sectionViewModel = self.dataArray[oriIndexpath.section];
TPColumnItemModel *model = sectionViewModel.dataArray[oriIndexpath.item];
NSMutableArray *mArray = [sectionViewModel.dataArray mutableCopy];
[mArray removeObject:model];
[mArray insertObject:model atIndex:currentPath.row];
sectionViewModel.dataArray = mArray.copy;
[self.collectionView moveItemAtIndexPath:oriIndexpath toIndexPath:currentPath];
oriIndexpath = currentPath;
}
break;
}
default:
{
if (!snapShotView) {
return;
}
UICollectionViewCell *OriCell = [self.collectionView cellForItemAtIndexPath:oriIndexpath];
UICollectionViewCell *currentCell = [self.collectionView cellForItemAtIndexPath:currentPath];
[UIView animateWithDuration:0.35 animations:^{
__block CGPoint snapShotCenter = OriCell.center;
snapShotView.center = snapShotCenter;
snapShotView.transform = CGAffineTransformIdentity;
snapShotView.alpha = 0.0f;
OriCell.hidden = NO;
OriCell.alpha =1.0f;
currentCell.hidden =NO;
currentCell.alpha = 1.0f;
} completion:^(BOOL finished) {
[snapShotView removeFromSuperview];
snapShotView = nil;
}];
NSMutableArray *array = [self.dataArray mutableCopy];
TPColumnItemModel *itemModel = array[fromIndex];
[array removeObject:itemModel];
[array insertObject:itemModel atIndex:currentPath.item];
self.dataArray = array;
break;
}
}
}
- (UIView *)customSnapshoFromView:(UIView *)inputView {
UIView *snapshot = UIView.new;
snapshot = [[UIView alloc]initWithFrame:inputView.frame];
UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 7.0);
[inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imageView = [[UIImageView alloc]initWithImage:viewImage];
[snapshot addSubview:imageView];
return snapshot;
}