實現(xiàn)原理
給UICollectionView添加一個手勢或者其他方式,在長按時手動觸發(fā)拖動開始,在移動過程中又要不斷手動地更新位置,而在完成時手動觸發(fā)完成操作,在取消時也要手動觸發(fā)取消移動操作。
而要實現(xiàn)移動,必須設(shè)置是否可移動。比如我們的demo中第一個分區(qū)是要求移動的,而第二個分區(qū)是不允許移動的,因此我們需要通過代理來設(shè)置是否可移動。
在移動完成時,會有代理回調(diào),此時我們要做的就是交換數(shù)據(jù)源來更新。
// 開始
- (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);
添加長按手勢
我們需要將手勢放到CollectionView上,因為我們要的是操作全范圍的!
self.collectionView.pagingEnabled = NO;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPressed:)];
[self.collectionView addGestureRecognizer:longPress];
手勢方法
- (void)onLongPressed:(UILongPressGestureRecognizer *)sender {
CGPoint point = [sender locationInView:sender.view];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
// 只允許第一區(qū)可移動
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;
}
}
}
是否允許移動
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
if (self.isEditing && indexPath.section == 0) {
return YES;
}
return NO;
}
完成移動更新源
- (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];
}
}
添加刪除移動效果
#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 {
// 選擇某一個
}
} 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 新聞欄目拖動重排添加刪除效果
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。