tableview的那些事儿tableview和cell相关TableView & Cell

MoveTableViewCell

2016-07-06  本文已影响58人  Evan叶少

MoveTableView:是一个长按拖动当前行切换位置

////  ViewController.m//  MoveTableViewCell//

//  Created by Evan on 16/7/6.

//  Copyright © 2016年 Evan. All rights reserved.

//#import "ViewController.h"

static NSString * const MoveTableViewCellID = @"MoveTableViewCellID";

@interface ViewController ()

@property (nonatomic, weak) UITableView *tableView;

@property (nonatomic , strong) NSMutableArray *objects;

@end

@implementation ViewController

#pragma mark - Data

- (NSMutableArray *)objects {

if (!_objects) {

_objects = [NSMutableArray array];

}

return _objects;

}

#pragma mark - Control

- (UITableView *)tableView {

if (!_tableView) {

UITableView *tableView = [[UITableView alloc] init];

tableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

tableView.rowHeight = 200.f;

[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:MoveTableViewCellID];

tableView.backgroundColor = [UIColor grayColor];

self.tableView = tableView;

[self.view addSubview:tableView];

}

return _tableView;

}

#pragma mark - LfileCycle

- (void)viewDidLoad {

[super viewDidLoad];

self.title = @"MoveTableViewCell";

NSMutableArray *listTop = [[NSMutableArray alloc] initWithArray:@[@"推荐",@"热点",@"杭州",@"社会",@"娱乐",@"科技",@"汽车",@"体育",@"订阅",@"财经",@"军事",@"国际",@"正能量",@"段子",@"趣图",@"美女",@"健康",@"教育",@"特卖",@"彩票",@"辟谣"]];

self.objects = listTop;

self.tableView.delegate = self;

self.tableView.dataSource = self;

// 给tableView添加手势

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognized:)];

[self.tableView addGestureRecognizer:longPress];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonPressed:)];

}

#pragma mark - Touch Event

- (void)longPressGestureRecognized:(id)sender {  

UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;// 手势

UIGestureRecognizerState state = longPress.state;// 触摸事件

CGPoint location = [longPress locationInView:self.tableView];// 获取触摸事件的位置

NSIndexPath *indextPath = [self.tableView indexPathForRowAtPoint:location];// 获取位置当前的行

static UIView *snapshot = nil;// 行用户的快照正在移动View。

static NSIndexPath *sourceIndexPath = nil;//初始索引路径,在那里手势开始。

switch (state) {

case UIGestureRecognizerStateBegan: {// 长按状态

if (indextPath) {

sourceIndexPath = indextPath;

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indextPath];

snapshot = [self customSnapshoFromView:cell];

__block CGPoint center = cell.center;// 获取当前Cell的中心点

snapshot.center = center;

snapshot.alpha = 0.0;

[self.tableView addSubview:snapshot];// 把映照View添加到tableView中

[UIView animateWithDuration:0.5 animations:^{

center.y = location.y;

snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);

snapshot.alpha = 0.98;

cell.alpha = 0;

cell.hidden = YES;

}];

}

}

case UIGestureRecognizerStateChanged: {// 移动状态

CGPoint center = snapshot.center;

center.y = location.y;

snapshot.center = center;

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

[self.objects exchangeObjectAtIndex:indextPath.row withObjectAtIndex:sourceIndexPath.row];// 交换对象索引

[self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indextPath];// 移动tableiewCell

sourceIndexPath = indextPath;

}

}

break;

default:

{

// Clean up.

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indextPath];

cell.alpha = 0.0;

[UIView animateWithDuration:0.5 animations:^{

snapshot.center = cell.center;

snapshot.transform = CGAffineTransformIdentity;

snapshot.alpha = 0.0;

cell.alpha = 1.0;

} completion:^(BOOL finished) {

cell.hidden = NO;

sourceIndexPath = nil;

[snapshot removeFromSuperview];

snapshot = nil;

}];

}

break;

}

}

#pragma mark - Layout

// 长按时候出现Cell效果

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

// 用户界面图形开始图像上下文与选项

UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 0);

[inputView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIView *snapshot = [[UIImageView alloc] initWithImage:image];

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;

}

#pragma mark - UItableView Dategate && DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return _objects.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MoveTableViewCellID forIndexPath:indexPath];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

cell.textLabel.text = _objects[indexPath.row];

return cell;

}

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

/**< 这里会告诉你删除第几行,然后删除,刷新 */

[self.objects removeObjectAtIndex:indexPath.row];

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {

return @"删除";

}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

return YES;

}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

CATransform3D transform = CATransform3DMakeScale(0.5, 0.5, 1.0);

cell.layer.transform = transform;

[UIView beginAnimations:@"transform" context:NULL];

[UIView setAnimationDuration:0.5];

cell.layer.transform = CATransform3DIdentity;

cell.alpha = 1.0;

cell.layer.shadowOffset = CGSizeMake(0, 0);// 回到原点

[UIView commitAnimations];

}

@end

github:https://github.com/EvanYeShao/MoveTableViewCell

上一篇 下一篇

猜你喜欢

热点阅读