UICollectionView 拖動排序

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

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末忱辅,一起剝皮案震驚了整個濱河市转绷,隨后出現(xiàn)的幾起案子迹卢,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,843評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異涨颜,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)茧球,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,538評論 3 392
  • 文/潘曉璐 我一進(jìn)店門庭瑰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人抢埋,你說我怎么就攤上這事弹灭。” “怎么了揪垄?”我有些...
    開封第一講書人閱讀 163,187評論 0 353
  • 文/不壞的土叔 我叫張陵穷吮,是天一觀的道長。 經(jīng)常有香客問我饥努,道長捡鱼,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,264評論 1 292
  • 正文 為了忘掉前任肪凛,我火速辦了婚禮堰汉,結(jié)果婚禮上辽社,老公的妹妹穿的比我還像新娘伟墙。我一直安慰自己翘鸭,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,289評論 6 390
  • 文/花漫 我一把揭開白布戳葵。 她就那樣靜靜地躺著就乓,像睡著了一般。 火紅的嫁衣襯著肌膚如雪拱烁。 梳的紋絲不亂的頭發(fā)上生蚁,一...
    開封第一講書人閱讀 51,231評論 1 299
  • 那天,我揣著相機(jī)與錄音戏自,去河邊找鬼邦投。 笑死,一個胖子當(dāng)著我的面吹牛擅笔,可吹牛的內(nèi)容都是我干的志衣。 我是一名探鬼主播,決...
    沈念sama閱讀 40,116評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼猛们,長吁一口氣:“原來是場噩夢啊……” “哼念脯!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起弯淘,我...
    開封第一講書人閱讀 38,945評論 0 275
  • 序言:老撾萬榮一對情侶失蹤绿店,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后庐橙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體假勿,經(jīng)...
    沈念sama閱讀 45,367評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,581評論 2 333
  • 正文 我和宋清朗相戀三年态鳖,在試婚紗的時候發(fā)現(xiàn)自己被綠了废登。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,754評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡郁惜,死狀恐怖堡距,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情兆蕉,我是刑警寧澤羽戒,帶...
    沈念sama閱讀 35,458評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站虎韵,受9級特大地震影響易稠,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜包蓝,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,068評論 3 327
  • 文/蒙蒙 一驶社、第九天 我趴在偏房一處隱蔽的房頂上張望企量。 院中可真熱鬧,春花似錦亡电、人聲如沸届巩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,692評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽恕汇。三九已至,卻和暖如春或辖,著一層夾襖步出監(jiān)牢的瞬間瘾英,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,842評論 1 269
  • 我被黑心中介騙來泰國打工颂暇, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留缺谴,地道東北人。 一個月前我還...
    沈念sama閱讀 47,797評論 2 369
  • 正文 我出身青樓耳鸯,卻偏偏與公主長得像湿蛔,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子片拍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,654評論 2 354

推薦閱讀更多精彩內(nèi)容