博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
拖拽cell 交换cell位置
阅读量:6282 次
发布时间:2019-06-22

本文共 6609 字,大约阅读时间需要 22 分钟。

#import "ViewController.h"@interface ViewController ()
@end@implementation ViewController{ UITableView * _tableView; NSMutableArray * _obj; }- (void)viewDidLoad { [super viewDidLoad]; _tableView = [[UITableView alloc]init]; _tableView.frame = self.view.bounds; [self.view addSubview:_tableView]; _tableView.delegate = self; _tableView.dataSource = self;// [_tableView setEditing:YES animated:YES]; UILongPressGestureRecognizer * longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longpress:)]; [_tableView addGestureRecognizer:longpress]; _obj = [NSMutableArray arrayWithCapacity:10]; for (int i = 0; i<50 ; i++) { [_obj addObject:[NSNumber numberWithInt:i]]; } _tableView.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]]; // [self tableView:nil moveRowAtIndexPath:nil toIndexPath:nil];}//-(BOOL)tableView:(UITableView *) tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath//{// //打开编辑// return YES;//}//- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath//{// //允许移动// return YES;// //return NO;//}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _obj.count;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; } cell.textLabel.text = [NSString stringWithFormat:@"%ld" , indexPath.row]; cell.contentView.backgroundColor = [UIColor lightGrayColor]; return cell;}- (void)longpress:(id)sender { UILongPressGestureRecognizer * longpress = (UILongPressGestureRecognizer *)sender; UIGestureRecognizerState state = longpress.state; CGPoint location = [longpress locationInView:_tableView]; NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location]; static UIView * snapshot = nil; static NSIndexPath * sourceIndexPath = nil; switch (state) { case UIGestureRecognizerStateBegan: if (indexPath) { sourceIndexPath = indexPath; UITableViewCell * cell = [_tableView cellForRowAtIndexPath:indexPath]; //截屏快照 snapshot = [self customSnapsFromView: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; case UIGestureRecognizerStateChanged:{ CGPoint center = snapshot.center; center.y = location.y; snapshot.center = center; if (indexPath && ![indexPath isEqual:sourceIndexPath]) { //交换数组元素的位置 [_obj exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];         //交换cell的位置 [_tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath]; sourceIndexPath = indexPath; } break; } default: { UITableViewCell * cell = [_tableView cellForRowAtIndexPath:indexPath]; [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; } } }-(UIView *)customSnapsFromView:(UIView *)inputView { UIView * snapshot = nil; //7.0一下的系统版本 截图快照 if ([[[UIDevice currentDevice]systemVersion]doubleValue]<7.0) { snapshot = [self customSnapShortFromViewEx:inputView]; }else{ 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; }-(UIView *)customSnapShortFromViewEx:(UIView *)inputView { CGSize inSize = inputView.bounds.size;

// 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕分辨率

UIGraphicsBeginImageContextWithOptions(inSize, NO, [UIScreen mainScreen].scale);        [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];        UIImage * image = UIGraphicsGetImageFromCurrentImageContext();        UIGraphicsEndImageContext();        UIImageView * snapshot = [[UIImageView alloc]initWithImage:image];        return snapshot;        }-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{        [tableView deselectRowAtIndexPath:indexPath animated:NO];    }-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{        return 64.0f;}@end

  

转载于:https://www.cnblogs.com/yuwei0911/p/5688887.html

你可能感兴趣的文章
面试题 16:反转链表
查看>>
action类型的按钮和object按钮的用法
查看>>
并查集 4104 这是一棵树吗
查看>>
servlet流(转摘)
查看>>
Android 连接 SQL Server (jtds方式)——上
查看>>
折射向量计算(Refraction Vector Calculation)
查看>>
常见的压缩文件格式案例tarZ
查看>>
QT环境下实现UI界面的“拼图游戏”
查看>>
WIN7 64位操作系统 无法找到Access驱动
查看>>
怎样解题
查看>>
HDU - 4901 The Romantic Hero(dp)
查看>>
LightOJ - 1246 Colorful Board(DP+组合数)
查看>>
sqlserver distribution分发alwayson搭建
查看>>
力扣算法题—055跳跃游戏
查看>>
Nginx服务安装指南
查看>>
layer遮罩层 简单的遮罩层
查看>>
vue中使用腾讯云Im
查看>>
java FTP上传文件
查看>>
ubuntu 安装 eslint
查看>>
【转】围观 Joomla, Wordpress 和 Drupal
查看>>