看到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