cell拖動(dòng)
- UITableView - cell之間拖動(dòng) 下次在進(jìn)入APP 自動(dòng)記錄此次位置! 網(wǎng)絡(luò)請求數(shù)據(jù)進(jìn)行排序處理! (cell自定義)直接VC引用此View
核心代碼如下:
.h中
#import <UIKit/UIKit.h>
@class RTDragCellTableView;
@protocol RTDragCellTableViewDataSource <UITableViewDataSource>
@required
/**將外部數(shù)據(jù)源數(shù)組傳入悼院,以便在移動(dòng)cell數(shù)據(jù)發(fā)生改變時(shí)進(jìn)行修改重排*/
- (NSArray *)originalArrayDataForTableView:(RTDragCellTableView *)tableView;
@end
@protocol RTDragCellTableViewDelegate <UITableViewDelegate>
@required
/**將修改重排后的數(shù)組傳入抵皱,以便外部更新數(shù)據(jù)源*/
- (void)tableView:(RTDragCellTableView *)tableView newArrayDataForDataSource:(NSArray *)newArray;
@optional
/**選中的cell準(zhǔn)備好可以移動(dòng)的時(shí)候*/
- (void)tableView:(RTDragCellTableView *)tableView cellReadyToMoveAtIndexPath:(NSIndexPath *)indexPath;
/**選中的cell正在移動(dòng),變換位置,手勢尚未松開*/
- (void)cellIsMovingInTableView:(RTDragCellTableView *)tableView;
/**選中的cell完成移動(dòng),手勢已松開*/
- (void)cellDidEndMovingInTableView:(RTDragCellTableView *)tableView;
@end
@interface RTDragCellTableView : UITableView
@property (nonatomic, assign) id<RTDragCellTableViewDataSource> dataSource;
@property (nonatomic, assign) id<RTDragCellTableViewDelegate> delegate;
@end
.m中
#import "RTDragCellTableView.h"
typedef enum{
RTSnapshotMeetsEdgeTop,
RTSnapshotMeetsEdgeBottom,
}RTSnapshotMeetsEdge;
@interface RTDragCellTableView ()
/**對被選中的cell的截圖*/
@property (nonatomic, weak) UIView *snapshot;
/**被選中的cell的原始位置*/
@property (nonatomic, strong) NSIndexPath *originalIndexPath;
/**被選中的cell的新位置*/
@property (nonatomic, strong) NSIndexPath *relocatedIndexPath;
/**cell被拖動(dòng)到邊緣后開啟,tableview自動(dòng)向上或向下滾動(dòng)*/
@property (nonatomic, strong) CADisplayLink *autoScrollTimer;
/**記錄手指所在的位置*/
@property (nonatomic, assign) CGPoint fingerLocation;
/**自動(dòng)滾動(dòng)的方向*/
@property (nonatomic, assign) RTSnapshotMeetsEdge autoScrollDirection;
@end
@implementation RTDragCellTableView
@dynamic delegate;
@dynamic dataSource;
# pragma mark - initialization methods
/**在初始化時(shí)加入一個(gè)長按手勢*/
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
self = [super initWithFrame:frame style:style];
if (self) {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGestureRecognized:)];
[self addGestureRecognizer:longPress];
}
return self;
}
/**在初始化時(shí)加入一個(gè)長按手勢*/
//- (instancetype)init{
// self = [super init];
// if (self) {
// UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGestureRecognized:)];
// [self addGestureRecognizer:longPress];
// }
// return self;
//}
# pragma mark - Gesture methods
- (void)longPressGestureRecognized:(id)sender{
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
UIGestureRecognizerState longPressState = longPress.state;
//手指在tableView中的位置
_fingerLocation = [longPress locationInView:self];
//手指按住位置對應(yīng)的indexPath,可能為nil
_relocatedIndexPath = [self indexPathForRowAtPoint:_fingerLocation];
switch (longPressState) {
case UIGestureRecognizerStateBegan:{ //手勢開始,對被選中cell截圖纯路,隱藏原cell
_originalIndexPath = [self indexPathForRowAtPoint:_fingerLocation];
if (_originalIndexPath) {
[self cellSelectedAtIndexPath:_originalIndexPath];
}
break;
}
case UIGestureRecognizerStateChanged:{//點(diǎn)擊位置移動(dòng),判斷手指按住位置是否進(jìn)入其它indexPath范圍寞忿,若進(jìn)入則更新數(shù)據(jù)源并移動(dòng)cell
//截圖跟隨手指移動(dòng)
CGPoint center = _snapshot.center;
center.y = _fingerLocation.y;
_snapshot.center = center;
if ([self checkIfSnapshotMeetsEdge]) {
[self startAutoScrollTimer];
}else{
[self stopAutoScrollTimer];
}
//手指按住位置對應(yīng)的indexPath驰唬,可能為nil
_relocatedIndexPath = [self indexPathForRowAtPoint:_fingerLocation];
if (_relocatedIndexPath && ![_relocatedIndexPath isEqual:_originalIndexPath]) {
[self cellRelocatedToNewIndexPath:_relocatedIndexPath];
}
break;
}
default: { //長按手勢結(jié)束或被取消,移除截圖罐脊,顯示cell
[self stopAutoScrollTimer];
[self didEndDraging];
break;
}
}
}
# pragma mark - timer methods
/**
* 創(chuàng)建定時(shí)器并運(yùn)行
*/
- (void)startAutoScrollTimer{
if (!_autoScrollTimer) {
_autoScrollTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(startAutoScroll)];
[_autoScrollTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
}
/**
* 停止定時(shí)器并銷毀
*/
- (void)stopAutoScrollTimer{
if (_autoScrollTimer) {
[_autoScrollTimer invalidate];
_autoScrollTimer = nil;
}
}
# pragma mark - Private methods
/**修改數(shù)據(jù)源定嗓,通知外部更新數(shù)據(jù)源*/
- (void)updateDataSource{
//通過DataSource代理獲得原始數(shù)據(jù)源數(shù)組
NSMutableArray *tempArray = [NSMutableArray array];
if ([self.dataSource respondsToSelector:@selector(originalArrayDataForTableView:)]) {
[tempArray addObjectsFromArray:[self.dataSource originalArrayDataForTableView:self]];
}
//判斷原始數(shù)據(jù)源是否為嵌套數(shù)組
if ([self nestedArrayCheck:tempArray]) {//是嵌套數(shù)組
if (_originalIndexPath.section == _relocatedIndexPath.section) {//在同一個(gè)section內(nèi)
[self moveObjectInMutableArray:tempArray[_originalIndexPath.section] fromIndex:_originalIndexPath.row toIndex:_relocatedIndexPath.row];
}else{ //不在同一個(gè)section內(nèi)
id originalObj = tempArray[_originalIndexPath.section][_originalIndexPath.item];
[tempArray[_relocatedIndexPath.section] insertObject:originalObj atIndex:_relocatedIndexPath.item];
[tempArray[_originalIndexPath.section] removeObjectAtIndex:_originalIndexPath.item];
}
}else{ //不是嵌套數(shù)組
[self moveObjectInMutableArray:tempArray fromIndex:_originalIndexPath.row toIndex:_relocatedIndexPath.row];
}
//將新數(shù)組傳出外部以更改數(shù)據(jù)源
if ([self.delegate respondsToSelector:@selector(tableView:newArrayDataForDataSource:)]) {
[self.delegate tableView:self newArrayDataForDataSource:tempArray];
}
}
/**
* 檢查數(shù)組是否為嵌套數(shù)組
* @param array 需要被檢測的數(shù)組
* @return 返回YES則表示是嵌套數(shù)組
*/
- (BOOL)nestedArrayCheck:(NSArray *)array{
for (id obj in array) {
if ([obj isKindOfClass:[NSArray class]]) {
return YES;
}
}
return NO;
}
/**
* cell被長按手指選中,對其進(jìn)行截圖萍桌,原cell隱藏
*/
- (void)cellSelectedAtIndexPath:(NSIndexPath *)indexPath{
//HomePageTableViewCell.h
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath];
UIView *snapshot = [self customSnapshotFromView:cell];
[self addSubview:snapshot];
_snapshot = snapshot;
cell.hidden = YES;
CGPoint center = _snapshot.center;
center.y = _fingerLocation.y;
[UIView animateWithDuration:0.2 animations:^{
_snapshot.transform = CGAffineTransformMakeScale(1.03, 1.03);
_snapshot.alpha = 0.98;
_snapshot.center = center;
}];
}
/**
* 截圖被移動(dòng)到新的indexPath范圍宵溅,這時(shí)先更新數(shù)據(jù)源,重排數(shù)組上炎,再將cell移至新位置
* @param indexPath 新的indexPath
*/
- (void)cellRelocatedToNewIndexPath:(NSIndexPath *)indexPath{
//更新數(shù)據(jù)源并返回給外部
[self updateDataSource];
//交換移動(dòng)cell位置
[self moveRowAtIndexPath:_originalIndexPath toIndexPath:indexPath];
//更新cell的原始indexPath為當(dāng)前indexPath
_originalIndexPath = indexPath;
}
/**
* 拖拽結(jié)束恃逻,顯示cell雏搂,并移除截圖
*/
- (void)didEndDraging{
UITableViewCell *cell = [self cellForRowAtIndexPath:_originalIndexPath];
cell.hidden = NO;
cell.alpha = 0;
[UIView animateWithDuration:0.2 animations:^{
_snapshot.center = cell.center;
_snapshot.alpha = 0;
_snapshot.transform = CGAffineTransformIdentity;
cell.alpha = 1;
} completion:^(BOOL finished) {
cell.hidden = NO;
[_snapshot removeFromSuperview];
_snapshot = nil;
_originalIndexPath = nil;
_relocatedIndexPath = nil;
}];
}
/** 返回一個(gè)給定view的截圖. */
- (UIView *)customSnapshotFromView:(UIView *)inputView {
// Make an image from the input view.
UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 0);
[inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Create an image view.
UIView *snapshot = [[UIImageView alloc] initWithImage:image];
snapshot.center = inputView.center;
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;
}
/**
* 將可變數(shù)組中的一個(gè)對象移動(dòng)到該數(shù)組中的另外一個(gè)位置
* @param array 要變動(dòng)的數(shù)組
* @param fromIndex 從這個(gè)index
* @param toIndex 移至這個(gè)index
*/
- (void)moveObjectInMutableArray:(NSMutableArray *)array fromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex{
if (fromIndex < toIndex) {
for (NSInteger i = fromIndex; i < toIndex; i ++) {
[array exchangeObjectAtIndex:i withObjectAtIndex:i + 1];
}
}else{
for (NSInteger i = fromIndex; i > toIndex; i --) {
[array exchangeObjectAtIndex:i withObjectAtIndex:i - 1];
}
}
}
/**
* 檢查截圖是否到達(dá)邊緣,并作出響應(yīng)
*/
- (BOOL)checkIfSnapshotMeetsEdge{
CGFloat minY = CGRectGetMinY(_snapshot.frame);
CGFloat maxY = CGRectGetMaxY(_snapshot.frame);
if (minY < self.contentOffset.y) {
_autoScrollDirection = RTSnapshotMeetsEdgeTop;
return YES;
}
if (maxY > self.bounds.size.height + self.contentOffset.y) {
_autoScrollDirection = RTSnapshotMeetsEdgeBottom;
return YES;
}
return NO;
}
/**
* 開始自動(dòng)滾動(dòng)
*/
- (void)startAutoScroll{
CGFloat pixelSpeed = 4;
if (_autoScrollDirection == RTSnapshotMeetsEdgeTop) {//向下滾動(dòng)
if (self.contentOffset.y > 0) {//向下滾動(dòng)最大范圍限制
[self setContentOffset:CGPointMake(0, self.contentOffset.y - pixelSpeed)];
_snapshot.center = CGPointMake(_snapshot.center.x, _snapshot.center.y - pixelSpeed);
}
}else{ //向上滾動(dòng)
if (self.contentOffset.y + self.bounds.size.height < self.contentSize.height) {//向下滾動(dòng)最大范圍限制
[self setContentOffset:CGPointMake(0, self.contentOffset.y + pixelSpeed)];
_snapshot.center = CGPointMake(_snapshot.center.x, _snapshot.center.y + pixelSpeed);
}
}
/* 當(dāng)把截圖拖動(dòng)到邊緣寇损,開始自動(dòng)滾動(dòng)凸郑,如果這時(shí)手指完全不動(dòng),則不會(huì)觸發(fā)‘UIGestureRecognizerStateChanged’矛市,對應(yīng)的代碼就不會(huì)執(zhí)行芙沥,導(dǎo)致雖然截圖在tableView中的位置變了,但并沒有移動(dòng)那個(gè)隱藏的cell浊吏,用下面代碼可解決此問題而昨,cell會(huì)隨著截圖的移動(dòng)而移動(dòng)
*/
_relocatedIndexPath = [self indexPathForRowAtPoint:_snapshot.center];
if (_relocatedIndexPath && ![_relocatedIndexPath isEqual:_originalIndexPath]) {
[self cellRelocatedToNewIndexPath:_relocatedIndexPath];
}
}
@end