MoveTableViewCell

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容