UICollectionView 拖動(dòng)排序

創(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);

拖動(dòng)排序主要使用的是手勢(shì)

// 添加長(zhǎng)按手勢(shì)

UILongPressGestureRecognizer*longPress?=?[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(handlelongGesture:)];

[collectionViewaddGestureRecognizer:longPress];

#pragma mark - 長(zhǎng)按手勢(shì)

-?(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移動(dòng)

returnYES;

}

-?(void)collectionView:(UICollectionView*)collectionViewmoveItemAtIndexPath:(NSIndexPath*)sourceIndexPathtoIndexPath:(NSIndexPath*)destinationIndexPath

{

//取出移動(dòng)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:

{//手勢(shì)開始

//判斷手勢(shì)落點(diǎn)位置是否在row上

NSIndexPath*indexPath?=?[self.collectionViewindexPathForItemAtPoint:[longPresslocationInView:self.collectionView]];

if(indexPath?==nil)?{

break;

}

UICollectionViewCell*cell?=?[self.collectionViewcellForItemAtIndexPath:indexPath];

[self.viewbringSubviewToFront:cell];

//iOS9方法?移動(dòng)cell

[self.collectionViewbeginInteractiveMovementForItemAtIndexPath:indexPath];

}

break;

caseUIGestureRecognizerStateChanged:

{//?手勢(shì)改變

//?iOS9方法?移動(dòng)過(guò)程中隨時(shí)更新cell位置

[self.collectionViewupdateInteractiveMovementTargetPosition:[longPresslocationInView:self.collectionView]];

}

break;

caseUIGestureRecognizerStateEnded:

{//?手勢(shì)結(jié)束

//?iOS9方法?移動(dòng)結(jié)束后關(guān)閉cell移動(dòng)

[self.collectionViewendInteractiveMovement];

}

break;

default://手勢(shì)其他狀態(tài)

[self.collectionViewcancelInteractiveMovement];

break;

}

}

#pragma mark - iOS9 之前的方法

-?(void)action:(UILongPressGestureRecognizer*)longPress

{

switch(longPress.state)?{

caseUIGestureRecognizerStateBegan:

{//?手勢(shì)開始

//判斷手勢(shì)落點(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:

{//?手勢(shì)改變

//當(dāng)前手指位置?截圖視圖位置隨著手指移動(dòng)而移動(dòng)

CGPoint?currentPoint?=?[longPresslocationInView:self.collectionView];

self.snapshotView.center=?currentPoint;

//?計(jì)算截圖視圖和哪個(gè)可見cell相交

for(UICollectionViewCell*cell?inself.collectionView.visibleCells)?{

//?當(dāng)前隱藏的cell就不需要交換了,直接continue

if([self.collectionViewindexPathForCell:cell]?==self.oldIndexPath)?{

continue;

}

//?計(jì)算中心距

CGFloat?space?=?sqrtf(pow(self.snapshotView.center.x-?cell.center.x,2)?+?powf(self.snapshotView.center.y-?cell.center.y,2));

//?如果相交一半就移動(dòng)

if(space?<=self.snapshotView.bounds.size.width/2)?{

self.moveIndexPath=?[self.collectionViewindexPathForCell:cell];

//移動(dòng)?會(huì)調(diào)用willMoveToIndexPath方法更新數(shù)據(jù)源

[self.collectionViewmoveItemAtIndexPath:self.oldIndexPathtoIndexPath:self.moveIndexPath];

//設(shè)置移動(dòng)后的起始indexPath

self.oldIndexPath=self.moveIndexPath;

break;

}

}

}

break;

default:

{//?手勢(shì)結(jié)束和其他狀態(tài)

UICollectionViewCell*cell?=?[self.collectionViewcellForItemAtIndexPath:self.oldIndexPath];

//?結(jié)束動(dòng)畫過(guò)程中停止交互,防止出問(wèn)題

self.collectionView.userInteractionEnabled=NO;

//?給截圖視圖一個(gè)動(dòng)畫移動(dòng)到隱藏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;

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容