實(shí)現(xiàn)原理
給UICollectionView添加一個(gè)手勢(shì)或者其他方式田弥,在長(zhǎng)按時(shí)手動(dòng)觸發(fā)拖動(dòng)開始涛酗,在移動(dòng)過(guò)程中又要不斷手動(dòng)地更新位置,而在完成時(shí)手動(dòng)觸發(fā)完成操作偷厦,在取消時(shí)也要手動(dòng)觸發(fā)取消移動(dòng)操作商叹。
而要實(shí)現(xiàn)移動(dòng),必須設(shè)置是否可移動(dòng)只泼。比如我們的demo中第一個(gè)分區(qū)是要求移動(dòng)的剖笙,而第二個(gè)分區(qū)是不允許移動(dòng)的,因此我們需要通過(guò)代理來(lái)設(shè)置是否可移動(dòng)请唱。
在移動(dòng)完成時(shí)弥咪,會(huì)有代理回調(diào),此時(shí)我們要做的就是交換數(shù)據(jù)源來(lái)更新十绑。
// 開始
- (BOOL)beginInteractiveMovementForItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
// 更新位置
- (void)updateInteractiveMovementTargetPosition:(CGPoint)targetPosition NS_AVAILABLE_IOS(9_0);
// 完成
- (void)endInteractiveMovement NS_AVAILABLE_IOS(9_0);
// 取消
- (void)cancelInteractiveMovement NS_AVAILABLE_IOS(9_0);
添加長(zhǎng)按手勢(shì)
我們需要將手勢(shì)放到CollectionView上聚至,因?yàn)槲覀円氖遣僮魅秶模?
self.collectionView.pagingEnabled = NO;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPressed:)];
[self.collectionView addGestureRecognizer:longPress];
手勢(shì)方法
- (void)onLongPressed:(UILongPressGestureRecognizer *)sender {
CGPoint point = [sender locationInView:sender.view];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
// 只允許第一區(qū)可移動(dòng)
if (indexPath.section != 0) {
return;
}
switch (sender.state) {
case UIGestureRecognizerStateBegan: {
if (indexPath) {
[self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
}
break;
}
case UIGestureRecognizerStateChanged: {
[self.collectionView updateInteractiveMovementTargetPosition:point];
break;
}
case UIGestureRecognizerStateEnded: {
[self.collectionView endInteractiveMovement];
break;
}
default: {
[self.collectionView cancelInteractiveMovement];
break;
}
}
}
是否允許移動(dòng)
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
if (self.isEditing && indexPath.section == 0) {
return YES;
}
return NO;
}
完成移動(dòng)更新源
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
if (sourceIndexPath.section == 0 && destinationIndexPath.section == 0) {
[self.firstList exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
}
}
添加刪除移動(dòng)效果
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
if (self.isEditing) {
TestModel *model = [self.secondList hyb_objectAtIndex:indexPath.item];
[self.firstList addObject:model];
[self.secondList removeObject:model];
NSInteger index = self.firstList.count - 1;
if (self.firstList.count == 0) {
index = 0;
}
TestColumnCell *cell = (TestColumnCell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.isEditing = NO;
[collectionView moveItemAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForItem:index inSection:1]];
[self saveColumns];
} else {
// 選擇某一個(gè)
}
} else {
TestModel *model = [self.firstList hyb_objectAtIndex:indexPath.item];
[self.secondList addObject:model];
[self.firstList removeObject:model];
NSInteger index = self.secondList.count - 1;
if (self.secondList.count == 0) {
index = 0;
}
if (self.isEditing) {
TestColumnCell *cell = (TestColumnCell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.isEditing = YES;
}
[collectionView moveItemAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
[self saveColumns];
}
[collectionView reloadData];
}
iOS 新聞欄目拖動(dòng)重排添加刪除效果
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)骚揍,“玉大人字管,你說(shuō)我怎么就攤上這事⌒挪唬” “怎么了嘲叔?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)抽活。 經(jīng)常有香客問(wèn)我硫戈,道長(zhǎng),這世上最難降的妖魔是什么下硕? 我笑而不...
- 正文 為了忘掉前任丁逝,我火速辦了婚禮,結(jié)果婚禮上梭姓,老公的妹妹穿的比我還像新娘霜幼。我一直安慰自己,他們只是感情好誉尖,可當(dāng)我...
- 文/花漫 我一把揭開白布罪既。 她就那樣靜靜地躺著,像睡著了一般铡恕。 火紅的嫁衣襯著肌膚如雪琢感。 梳的紋絲不亂的頭發(fā)上,一...
- 那天没咙,我揣著相機(jī)與錄音猩谊,去河邊找鬼。 笑死祭刚,一個(gè)胖子當(dāng)著我的面吹牛牌捷,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播涡驮,決...
- 文/蒼蘭香墨 我猛地睜開眼暗甥,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了捉捅?” 一聲冷哼從身側(cè)響起撤防,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎棒口,沒(méi)想到半個(gè)月后寄月,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體辜膝,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年漾肮,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了厂抖。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站扮念,受9級(jí)特大地震影響损搬,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜扔亥,卻給世界環(huán)境...
- 文/蒙蒙 一场躯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧旅挤,春花似錦踢关、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至柒瓣,卻和暖如春儒搭,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背芙贫。 一陣腳步聲響...
- 正文 我出身青樓魂仍,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親拣挪。 傳聞我的和親對(duì)象是個(gè)殘疾皇子擦酌,可洞房花燭夜當(dāng)晚...