MoveTableViewCell

MoveTableView:是一個(gè)長(zhǎng)按拖動(dòng)當(dāng)前行切換位置

////? 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:@[@"推薦",@"熱點(diǎn)",@"杭州",@"社會(huì)",@"娛樂(lè)",@"科技",@"汽車(chē)",@"體育",@"訂閱",@"財(cái)經(jīng)",@"軍事",@"國(guó)際",@"正能量",@"段子",@"趣圖",@"美女",@"健康",@"教育",@"特賣(mài)",@"彩票",@"辟謠"]];

self.objects = listTop;

self.tableView.delegate = self;

self.tableView.dataSource = self;

// 給tableView添加手勢(shì)

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;// 手勢(shì)

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

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

NSIndexPath *indextPath = [self.tableView indexPathForRowAtPoint:location];// 獲取位置當(dāng)前的行

static UIView *snapshot = nil;// 行用戶的快照正在移動(dòng)View。

static NSIndexPath *sourceIndexPath = nil;//初始索引路徑,在那里手勢(shì)開(kāi)始。

switch (state) {

case UIGestureRecognizerStateBegan: {// 長(zhǎng)按狀態(tài)

if (indextPath) {

sourceIndexPath = indextPath;

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

snapshot = [self customSnapshoFromView:cell];

__block CGPoint center = cell.center;// 獲取當(dāng)前Cell的中心點(diǎn)

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: {// 移動(dòng)狀態(tài)

CGPoint center = snapshot.center;

center.y = location.y;

snapshot.center = center;

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

[self.objects exchangeObjectAtIndex:indextPath.row withObjectAtIndex:sourceIndexPath.row];// 交換對(duì)象索引

[self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indextPath];// 移動(dòng)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

// 長(zhǎng)按時(shí)候出現(xiàn)Cell效果

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

// 用戶界面圖形開(kāi)始圖像上下文與選項(xiàng)

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{

/**< 這里會(huì)告訴你刪除第幾行,然后刪除,刷新 */

[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);// 回到原點(diǎn)

[UIView commitAnimations];

}

@end


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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容