創(chuàng)建UICollectionView----
iOS9 添加的API
- (BOOL)beginInteractiveMovementForItemAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(9_0);// returns NO if reordering was prevented from beginning - otherwise YES
-?(void)updateInteractiveMovementTargetPosition:(CGPoint)targetPosition?NS_AVAILABLE_IOS(9_0);
-?(void)endInteractiveMovement?NS_AVAILABLE_IOS(9_0);
-?(void)cancelInteractiveMovement?NS_AVAILABLE_IOS(9_0);
拖動排序主要使用的是手勢
// 添加長按手勢
UILongPressGestureRecognizer*longPress?=?[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(handlelongGesture:)];
[collectionViewaddGestureRecognizer:longPress];
#pragma mark - 長按手勢
-?(void)handlelongGesture:(UILongPressGestureRecognizer*)longPress
{
if([[[UIDevicecurrentDevice]systemVersion]floatValue]?<9.0)?{
[selfaction:longPress];
}else{
[selfiOS9_Action:longPress];
}
}
#pragma mark - iOS9 之后的方法
-?(BOOL)collectionView:(UICollectionView*)collectionViewcanMoveItemAtIndexPath:(NSIndexPath*)indexPath
{
//?返回YES允許row移動
returnYES;
}
-?(void)collectionView:(UICollectionView*)collectionViewmoveItemAtIndexPath:(NSIndexPath*)sourceIndexPathtoIndexPath:(NSIndexPath*)destinationIndexPath
{
//取出移動row數(shù)據(jù)
idcolor?=self.dataArr[sourceIndexPath.row];
//從數(shù)據(jù)源中移除該數(shù)據(jù)
[self.dataArrremoveObject:color];
//將數(shù)據(jù)插入到數(shù)據(jù)源中的目標(biāo)位置
[self.dataArrinsertObject:coloratIndex:destinationIndexPath.row];
}
-?(void)iOS9_Action:(UILongPressGestureRecognizer*)longPress
{
switch(longPress.state)?{
caseUIGestureRecognizerStateBegan:
{//手勢開始
//判斷手勢落點(diǎn)位置是否在row上
NSIndexPath*indexPath?=?[self.collectionViewindexPathForItemAtPoint:[longPresslocationInView:self.collectionView]];
if(indexPath?==nil)?{
break;
}
UICollectionViewCell*cell?=?[self.collectionViewcellForItemAtIndexPath:indexPath];
[self.viewbringSubviewToFront:cell];
//iOS9方法?移動cell
[self.collectionViewbeginInteractiveMovementForItemAtIndexPath:indexPath];
}
break;
caseUIGestureRecognizerStateChanged:
{//?手勢改變
//?iOS9方法?移動過程中隨時更新cell位置
[self.collectionViewupdateInteractiveMovementTargetPosition:[longPresslocationInView:self.collectionView]];
}
break;
caseUIGestureRecognizerStateEnded:
{//?手勢結(jié)束
//?iOS9方法?移動結(jié)束后關(guān)閉cell移動
[self.collectionViewendInteractiveMovement];
}
break;
default://手勢其他狀態(tài)
[self.collectionViewcancelInteractiveMovement];
break;
}
}
#pragma mark - iOS9 之前的方法
-?(void)action:(UILongPressGestureRecognizer*)longPress
{
switch(longPress.state)?{
caseUIGestureRecognizerStateBegan:
{//?手勢開始
//判斷手勢落點(diǎn)位置是否在row上
NSIndexPath*indexPath?=?[self.collectionViewindexPathForItemAtPoint:[longPresslocationInView:self.collectionView]];
self.oldIndexPath=?indexPath;
if(indexPath?==nil)?{
break;
}
UICollectionViewCell*cell?=?[self.collectionViewcellForItemAtIndexPath:indexPath];
//?使用系統(tǒng)的截圖功能,得到cell的截圖視圖
UIView*snapshotView?=?[cellsnapshotViewAfterScreenUpdates:NO];
snapshotView.frame=?cell.frame;
[self.viewaddSubview:self.snapshotView=snapshotView];
//?截圖后隱藏當(dāng)前cell
cell.hidden=YES;
CGPoint?currentPoint?=?[longPresslocationInView:self.collectionView];
[UIViewanimateWithDuration:0.25animations:^{
snapshotView.transform=?CGAffineTransformMakeScale(1.05,1.05);
snapshotView.center=?currentPoint;
}];
}
break;
caseUIGestureRecognizerStateChanged:
{//?手勢改變
//當(dāng)前手指位置?截圖視圖位置隨著手指移動而移動
CGPoint?currentPoint?=?[longPresslocationInView:self.collectionView];
self.snapshotView.center=?currentPoint;
//?計算截圖視圖和哪個可見cell相交
for(UICollectionViewCell*cell?inself.collectionView.visibleCells)?{
//?當(dāng)前隱藏的cell就不需要交換了,直接continue
if([self.collectionViewindexPathForCell:cell]?==self.oldIndexPath)?{
continue;
}
//?計算中心距
CGFloat?space?=?sqrtf(pow(self.snapshotView.center.x-?cell.center.x,2)?+?powf(self.snapshotView.center.y-?cell.center.y,2));
//?如果相交一半就移動
if(space?<=self.snapshotView.bounds.size.width/2)?{
self.moveIndexPath=?[self.collectionViewindexPathForCell:cell];
//移動?會調(diào)用willMoveToIndexPath方法更新數(shù)據(jù)源
[self.collectionViewmoveItemAtIndexPath:self.oldIndexPathtoIndexPath:self.moveIndexPath];
//設(shè)置移動后的起始indexPath
self.oldIndexPath=self.moveIndexPath;
break;
}
}
}
break;
default:
{//?手勢結(jié)束和其他狀態(tài)
UICollectionViewCell*cell?=?[self.collectionViewcellForItemAtIndexPath:self.oldIndexPath];
//?結(jié)束動畫過程中停止交互,防止出問題
self.collectionView.userInteractionEnabled=NO;
//?給截圖視圖一個動畫移動到隱藏cell的新位置
[UIViewanimateWithDuration:0.25animations:^{
self.snapshotView.center=?cell.center;
self.snapshotView.transform=?CGAffineTransformMakeScale(1.0,1.0);
}completion:^(BOOLfinished)?{
//?移除截圖視圖,顯示隱藏的cell并開始交互
[self.snapshotViewremoveFromSuperview];
cell.hidden=NO;
self.collectionView.userInteractionEnabled=YES;
}];
}
break;
}
}