美文网首页
iOS 新闻栏目拖动重排添加删除效果

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

作者: 安静就好_ | 来源:发表于2016-09-19 17:05 被阅读131次
    实现原理
    
    给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];
    }
     
    

    相关文章

      网友评论

          本文标题:iOS 新闻栏目拖动重排添加删除效果

          本文链接:https://www.haomeiwen.com/subject/nwjvettx.html