iOS长按移动Table View Cells

文章目录前言实现思路代码完善`cell`未居中显示问题在系统版本为iOS9.x时,输入框会上一段距离问题切换输入法时,输入框被键盘遮住问题总结前言最近参与了事务流程工具化组件的开发,其中有一个模块需要通过长按移动Table View Cells,来达到调整任务的需求,在此记录下开发过程中的实现思路。完成后的效果如下图所示:实现思路添加手势首先给 collection view 添加一...
摘要由CSDN通过智能技术生成

前言

最近参与了事务流程工具化组件的开发,其中有一个模块需要通过长按移动Table View Cells,来达到调整任务的需求,在此记录下开发过程中的实现思路。完成后的效果如下图所示:

长按移动cell.gif

实现思路
  • 添加手势
    首先给 collection view 添加一个 UILongGestureRecognizer,在项目中一般使用懒加载的方式来对对象进行初始化:
- (UICollectionView *)collectionView {
   
    if (!_collectionView) {
   
        _collectionView =  [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        
        [_collectionView registerClass:[TLCMainCollectionViewCell class] forCellWithReuseIdentifier:[TLCMainCollectionViewCell identifier]];
        
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.bounces = YES;
        _collectionView.decelerationRate = 0;
        
        [_collectionView addGestureRecognizer:self.longPress];
    }
    return _collectionView;
}
- (UILongPressGestureRecognizer *)longPress {
   
    if (!_longPress) {
   
        _longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognized:)];
    }
    return _longPress;
}

在用户长按后,触犯长按事件,先获取到当前手势所在的collection view位置,再做后续的处理。

- (void)longPressGestureRecognized:(UILongPressGestureRecognizer *)sender {
   
    
    CGPoint location = [sender locationInView:sender.view];
    
    UIGestureRecognizerState state = sender.state;
    switch (state) {
   
            case UIGestureRecognizerStateBegan: {
   
                [self handleLongPressStateBeganWithLocation:location];
            }
            break;
            
            case UIGestureRecognizerStateChanged: {
   
            }
            break;
            
            case UIGestureRecognizerStateEnded:
            case UIGestureRecognizerStateCancelled: {
   
                [self longGestureEndedOrCancelledWithLocation:location];
            }
            break;
            
        default:
            break;
    }
}
  • 长按手势状态为开始
    主要处理两个方面的事务,一为获取当前长按手势所对应的Table View Cell的镜像,将其添加到 Collection View上。二为一些初始状态的设置,后续在移动后网络请求出错及判断当前手势所处的Table View和上一次是否一致需要使用到。最后调用startPageEdgeScroll开启定时器。
- (void)handleLongPressStateBeganWithLocation:(CGPoint)location {
   
    
    TLCMainCollectionViewCell *selectedCollectionViewCell = [self currentTouchedCollectionCellWithLocation:location];
    
    NSIndexPath *touchIndexPath = [self longGestureBeganIndexPathForRowAtPoint:location atTableView:selectedCollectionViewCell.tableView];
   
    if (!selectedCollectionViewCell || !touchIndexPath) {
   
        return ;
    }
    self.selectedCollectionViewCellRow = [self.collectionView indexPathForCell:selectedCollectionViewCell].row;
    
    // 已完成的任务,不支持排序
    TLPlanItem *selectedItem = [self.viewModel itemAtIndex:self.selectedCollectionViewCellRow
                                              subItemIndex:touchIndexPath.section];
    if (!selectedItem || selectedItem.finish) {
   
        return;
    }
    selectedItem.isHidden = YES;
    
    self.snapshotView = [self snapshotViewWithTableView:selectedCollectionViewCell.tableView
                                            atIndexPath:touchIndexPath];
    [self.collectionView addSubview:self.snapshotView];
    
    self.selectedIndexPath = touchIndexPath;
    self.originalSelectedIndexPathSection = touchIndexPath.section;
    self.originalCollectionViewCellRow = self.selectedCollectionViewCellRow;
    self.previousPoint = CGPointZero;
    
    [self startPageEdgeScroll];
}
  • 长按手势状态为改变
    longPressGestureRecognized方法中,可以发现,长按手势状态改变时,并未做任何的操作,主要原因是如果在此做Table View Cells的移动操作,如果数据超过一屏幕,无法自动将未在屏幕上的数据滚动显示出来。所以在长按手势状态为开始时,如果触摸点在Table View Cell上,开启定时器,来处理长按手势状态为改变时的情况。
- (void)startPageEdgeScroll {
   
    self.edgeScrollTimer = [CADisplayLink displayLinkWithTarget:
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值