iOS 新闻栏目拖动重排添加删除效果

实现原理

给UICollectionView添加一个手势或者其他方式,在长按时手动触发拖动开始,在移动过程中又要不断手动地更新位置,而在完成时手动触发完成操作,在取消时也要手动触发取消移动操作。

而要实现移动,必须设置是否可移动。比如我们的demo中第一个分区是要求移动的,而第二个分区是不允许移动的,因此我们需要通过代理来设置是否可移动。

在移动完成时,会有代理回调,此时我们要做的就是交换数据源来更新。

 
// 开始
- (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];
  
  // 只允许第一区可移动
  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];
}
 
复制代码

转载于:https://juejin.im/post/5a311bc06fb9a0450809a832

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值