美文网首页视图控件iOSiOS 进阶
iOS UITableView可以拖动排序的Cell

iOS UITableView可以拖动排序的Cell

作者: Apple技术产品粉 | 来源:发表于2017-07-23 11:15 被阅读117次

    效果图

    可拖动的Cell

    实现

    这个拖动效果可分为两种。第一种就是点击编辑按钮以后,按住右边的拖动按钮进行拖动。第二种就是不在编辑状态下,长按一个Cell即可进行拖动。

    先来说说第一种,第一种的方法比较简单,调用系统的方法即可。首先,当点击右上方的编辑按钮时,设置self.tableView.editing=!self.tableView.editing即可,当self.tableView.editing为True的时候,Cell上会自动出现一个拖动的按钮和一个删除的按钮,下面实现点击删除按钮和拖动按钮的方法即可。

    先说说删除按钮吧,只要实现- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath这一个代理方法即可,源码如下:

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

    //删除操作

    NSString *letter=[NSString stringWithFormat:@"%@",self.letterArr[indexPath.row]];

    [self.letterArr removeObject:letter];

    }

    [self.tableView reloadData];

    }

    非常简单吧?好,稍微复杂一点的就是拖动了。首先,要在代理方法里面实现- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath方法,将其设为YES,这样,Cell就可以进行拖动了。然后,实现代理方法-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath进行排序。源码如下:

    -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

    {

    // 取出要拖动的模型数据

    NSString *letter=[NSString stringWithFormat:@"%@",self.letterArr[sourceIndexPath.row]];

    //删除之前行的数据

    [self.letterArr removeObject:letter];

    // 插入数据到新的位置

    [self.letterArr insertObject:letter atIndex:destinationIndexPath.row];

    }

    这样就可以完成系统的拖动操作了。下面再说说长按实现拖动的方法。

    系统拖动时会有一个浮动的动画,我们怎么实现这个效果呢,答案就是创建一个快照。

    首先声明一个长按手势:

    UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;

    UIGestureRecognizerState state = longPress.state;

    根据state的值进行操作,当刚开始触摸的时候,我们就需要对当前cell创建一个快照:

    switch (state) {

    case UIGestureRecognizerStateBegan: {

    if (indexPath) {

    sourceIndexPath = indexPath;

    UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];

    snapshot = [self customSnapshoFromView:cell];

    __block CGPoint center = cell.center;

    snapshot.center = center;

    snapshot.alpha = 0.0;

    [_tableView addSubview:snapshot];

    [UIView animateWithDuration:0.25 animations:^{

    center.y = location.y;

    snapshot.center = center;

    snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);

    snapshot.alpha = 0.98;

    cell.alpha = 0.0f;

    } completion:^(BOOL finished) {

    cell.hidden = YES;

    }];

    }

    break;

    }

    然后看一下创建快照的方法,调用了UIView的snapshotViewAfterScreenUpdates:方法:

    - (UIView *)customSnapshoFromView:(UIView *)inputView {

    UIView* snapshot = nil;

    snapshot = [inputView snapshotViewAfterScreenUpdates:YES];

    snapshot.layer.masksToBounds = NO;

    snapshot.layer.cornerRadius = 0.0;

    snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);

    snapshot.layer.shadowRadius = 5.0;

    snapshot.layer.shadowOpacity = 0.4;

    return snapshot;

    }

    然后就是拖动中的方法了:

    case UIGestureRecognizerStateChanged: {

    CGPoint center = snapshot.center;

    center.y = location.y;

    snapshot.center = center;

    // Is destination valid and is it different from source?

    if (indexPath && ![indexPath isEqual:sourceIndexPath]) {

    [self.letterArr exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];

    [_tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];

    sourceIndexPath = indexPath;

    }

    break;

    }

    最后就是默认(拖动完成或者未拖动)方法:

    default: {

    // Clean up.

    UITableViewCell *cell = [_tableView cellForRowAtIndexPath:sourceIndexPath];

    [UIView animateWithDuration:0.25 animations:^{

    snapshot.center = cell.center;

    snapshot.transform = CGAffineTransformIdentity;

    snapshot.alpha = 0.0;

    cell.alpha = 1.0f;

    } completion:^(BOOL finished) {

    cell.hidden = NO;

    [snapshot removeFromSuperview];

    snapshot = nil;

    }];

    sourceIndexPath = nil;

    break;

    }

    这样,自定义的长按Cell拖动也就完成了,谢谢大家!

    相关文章

      网友评论

        本文标题:iOS UITableView可以拖动排序的Cell

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