IOS长按UITableViewCell进行移动
来源:互联网 发布:excel的数据分析工具 编辑:程序博客网 时间:2024/05/16 10:32
看到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
0 0
- IOS长按UITableViewCell进行移动
- iOS - 移动自定义UITableViewCell
- ios开发之实现长按UITableViewCell弹出UIMenuController
- ios开发之实现长按UITableViewCell弹出UIMenuController
- ios开发之实现长按UITableViewCell弹出UIMenuController
- iOS - UITableViewCell 添加长按手势(别的手势类似):
- ios开发之实现长按UITableViewCell弹出UIMenuController
- ios开发之实现长按UITableViewCell弹出UIMenuController
- ios开发之实现长按UITableViewCell弹出UIMenuController
- uitableviewcell 的长按事件
- uitableviewcell添加长按手势
- 长按UITableViewCell弹出UIMenuController
- UITableViewCell长按显示UIMenuController
- 长按UITableViewCell弹出UIMenuController
- ios开发之实现长按UITableViewCell弹出UIMenuController, 实现文字拷贝等操作
- ioS学习 UITableViewCell 增加、删除、移动
- UITableViewCell 的长按事件处理
- UITableViewCell 的长按事件处理
- Delphi XE6 for Android 我的第一个安卓程序,环境搭建
- Maven +Tomcat+m2eclipse的热部署(hot deploy)
- Git学习笔记(三)
- cobol学习之三文件的输入和输出
- MTK Android Driver:EINT
- IOS长按UITableViewCell进行移动
- 黑客高手是这样上网的!Vimium、Vimperator 浏览器插件让你全程用键盘快捷高效浏览网页
- [字典树] HDU 1004 - Let the Balloon Rise
- 00系统分析员 笔记2
- How to create a Maven web app and deploy to Tomcat - fast
- 3Sum Closest
- 关于sqlDataReader的问题
- firefox 中vim模式的操作--放弃鼠标
- Java中Comparable和Comparator