IOS长按UITableViewCell进行移动

本文介绍了如何在iOS应用中模仿天气APP的长按功能,允许用户通过长按UITableViewCell来移动其位置。主要步骤包括:设置UITableView,添加长按手势识别器,根据长按位置获取单元格,截图并随手指移动,更新IndexPath和表格视图,以及松手后处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看到iphone上自带的天气APP,可以长按UITableViewCell来移动位置,于是就模仿了一个。思路如下!

1:准备一个带测试数据的UITableView

2:给UITableView添加一个长按的事件,UILongPressGestureRecognizer

3:根据长按时获取到的位置来取得UITableViewCell

4:取得UITableViewCell生成一张截图,添加到view上,手指移动时,截图跟着移动

5:手指移动时,更新NSIndexPath,然后用moveRowAtIndexPath更新表格视图

6:手指松开后,删除截图

代码如下所示

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    dataArray = [@[@"列表1",@"列表2",@"列表3",@"列表4",@"列表5",@"列表6",@"列表7",@"列表8",@"列表9",@"列表10",@"列表11",@"列表12"]mutableCopy];
    myTableView = [[UITableView alloc]initWithFrame:self.view.frame];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    [self.view addSubview:myTableView];
    
    UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressHandler:)];
    [myTableView addGestureRecognizer:gesture];
}


-(void)longPressHandler:(id)sender{
    UILongPressGestureRecognizer *gesture = (UILongPressGestureRecognizer*)sender;
    CGPoint location = [gesture locationInView:myTableView];
    NSIndexPath *indexPath = [myTableView indexPathForRowAtPoint:location];//根据长按的点获取indexPath
    static UIView *snapshot = nil;//截图
    static NSIndexPath  *sourceIndexPath = nil;//根据手指的位置获取行
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan://长按时触发
        {
            NSLog(@"UIGestureRecognizerStateBegan");
            sourceIndexPath = indexPath;
            UITableViewCell *cellView = [myTableView cellForRowAtIndexPath:sourceIndexPath];
            //获取截图
            snapshot = [self customSnapshoFromView:cellView];
            [self.view addSubview:snapshot];
            __block CGPoint center = cellView.center;
            snapshot.center = center;
            snapshot.alpha = 0.0;
            [myTableView 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;
                cellView.alpha = 0.0;
                
            } completion:^(BOOL finished) {
                
                cellView.hidden = YES;
                
            }];

            break;
        }
            case UIGestureRecognizerStateChanged://移动
        {
            NSLog(@"UIGestureRecognizerStateChanged");
            //改变截图的位置
            CGPoint center = snapshot.center;
            center.y = location.y;
            snapshot.center = center;
            //更新列表
            if (indexPath&&![indexPath isEqual:sourceIndexPath]) {
                [dataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];
                [myTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];
                sourceIndexPath = indexPath;
            }
            break;
        }
        default://松开时移除截图
        {
            NSLog(@"default");
            UITableViewCell *cell = [myTableView cellForRowAtIndexPath:sourceIndexPath];
            cell.hidden = NO;
            cell.alpha = 0.0;
            
            [UIView animateWithDuration:0.25 animations:^{
                
                snapshot.center = cell.center;
                snapshot.transform = CGAffineTransformIdentity;
                snapshot.alpha = 0.0;
                cell.alpha = 1.0;
                
            } completion:^(BOOL finished) {
                
                sourceIndexPath = nil;
                [snapshot removeFromSuperview];
                snapshot = nil;
                
            }];

            break;
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *kIdentifier = @"Cell Identifier";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:kIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kIdentifier];
    }
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIView *)customSnapshoFromView:(UIView *)inputView {
    
    UIView *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;
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值