UItableView長ドラッグ順ソート(同業者高、異なるsection間交換をサポート)
10334 ワード
効果図:
DHDragableCellTableView.gif
githubダウンロードアドレス:DHDragableCellTable View
使用
DHDragableCellTableViewにtableViewを継承し、DHDragableCellTableView Data Source、DHDragableCellTableView Delegateに準拠
実装:
大まかな考え方では、UItable Viewにロングプッシュジェスチャーを追加し、ロングプッシュ後に選択したcellのスクリーンショットを与え、選択したcellを隠し、スクリーンショットをジェスチャーに従って移動させる.
1.ジェスチャーを追加
2.リスニングジェスチャーの状態
3.ドラッグ開始
4.ここをドラッグ_toBottomはintタイプでジェスチャーがどの方向にドラッグされているかを判断し、ドラッグされたcellと交換するcellの中心点を比較して交換するかどうかを判断します
5.交換データとcellの位置異なる行高の交換時にcellが変形しないように、交換後すぐにreloadDataをリリースし、2つのcellをスクリーンショットして、スクリーンショットで交換のアニメーションをシミュレートする
6.エッジスクロール処理
DHDragableCellTableView.gif
githubダウンロードアドレス:DHDragableCellTable View
使用
DHDragableCellTableViewにtableViewを継承し、DHDragableCellTableView Data Source、DHDragableCellTableView Delegateに準拠
#pragma mark - DHDragableCellTableViewDataSource
- (NSArray *)dataSourceArrayInTableView:(DHDragableCellTableView *)tableView{
return self.dataSource.copy;//
}
- (void)tableView:(DHDragableCellTableView *)tableView newDataSourceArrayAfterMove:(NSArray *)newDataSourceArray{
self.dataSource = newDataSourceArray.mutableCopy;//
[self.tableView reloadData];
}
実装:
大まかな考え方では、UItable Viewにロングプッシュジェスチャーを追加し、ロングプッシュ後に選択したcellのスクリーンショットを与え、選択したcellを隠し、スクリーンショットをジェスチャーに従って移動させる.
1.ジェスチャーを追加
/**
*/
- (void)dh_addGesture
{
_gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(dh_processGesture:)];
_gesture.minimumPressDuration = _gestureMinimumPressDuration;
[self addGestureRecognizer:_gesture];
}
2.リスニングジェスチャーの状態
- (void)dh_processGesture:(UILongPressGestureRecognizer *)gesture
{
switch (gesture.state) {
case UIGestureRecognizerStateBegan:
{
[self dh_gestureBegan:gesture];
}
break;
case UIGestureRecognizerStateChanged:
{
if (!_canEdgeScroll) {
[self dh_gestureChanged:gesture];
}
}
break;
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled:
{
[self dh_gestureEndedOrCancelled:gesture];
}
break;
default:
break;
}
}
3.ドラッグ開始
- (void)dh_gestureBegan:(UILongPressGestureRecognizer *)gesture
{
CGPoint point = [gesture locationInView:gesture.view];
self.lastPoint = point;
NSIndexPath *selectedIndexPath = [self indexPathForRowAtPoint:point];
if (!selectedIndexPath) {
return;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(tableView:willMoveCellAtIndexPath:)]) {
[self.delegate tableView:self willMoveCellAtIndexPath:selectedIndexPath];
}
if (_canEdgeScroll) {
//
[self dh_startEdgeScroll];
}
//
if (self.dataSource && [self.dataSource respondsToSelector:@selector(dataSourceArrayInTableView:)]) {
_tempDataSource = [self.dataSource dataSourceArrayInTableView:self].mutableCopy;
}
_selectedIndexPath = selectedIndexPath;
UITableViewCell *cell = [self cellForRowAtIndexPath:selectedIndexPath];
_tempView = [self dh_snapshotViewWithInputView:cell];
if (_drawMovalbeCellBlock) {
// _tempView block
_drawMovalbeCellBlock(_tempView);
}else {
//
_tempView.layer.shadowColor = [UIColor grayColor].CGColor;
_tempView.layer.masksToBounds = NO;
_tempView.layer.cornerRadius = 0;
_tempView.layer.shadowOffset = CGSizeMake(-5, 0);
_tempView.layer.shadowOpacity = 0.4;
_tempView.layer.shadowRadius = 5;
}
_tempView.frame = cell.frame;
[self addSubview:_tempView];
// cell
cell.hidden = YES;
[UIView animateWithDuration:kDH_DragableCellAnimationTime animations:^{
_tempView.center = CGPointMake(_tempView.center.x, point.y);
}];
}
4.ここをドラッグ_toBottomはintタイプでジェスチャーがどの方向にドラッグされているかを判断し、ドラッグされたcellと交換するcellの中心点を比較して交換するかどうかを判断します
- (void)dh_gestureChanged:(UILongPressGestureRecognizer *)gesture
{
CGPoint point = [gesture locationInView:gesture.view];
//
if (point.y - self.lastPoint.y > 0) {
_toBottom = 1;//
}else if(point.y - self.lastPoint.y < 0){
_toBottom = -1;//
}else{
_toBottom = 0;
}
self.lastPoint = point;
NSIndexPath *currentIndexPath = [self indexPathForRowAtPoint:point];
if (currentIndexPath && ![_selectedIndexPath isEqual:currentIndexPath]) {
UITableViewCell *cell = [self cellForRowAtIndexPath:_selectedIndexPath];
UITableViewCell *cell1 = [self cellForRowAtIndexPath:currentIndexPath];
// cell cell centerY
if ((_toBottom == 1 && (point.y+cell.frame.size.height/2) >= CGRectGetMaxY(cell1.frame) && (CGRectGetMaxY(cell1.frame) >= CGRectGetMaxY(cell.frame))) || ((_toBottom == -1 && (point.y-cell.frame.size.height/2) <= CGRectGetMinY(cell1.frame)) && (CGRectGetMinY(cell1.frame) <= CGRectGetMinY(cell.frame)))) {
// cell
[self dh_updateDataSourceAndCellFromIndexPath:_selectedIndexPath toIndexPath:currentIndexPath];
if (self.delegate && [self.delegate respondsToSelector:@selector(tableView:didMoveCellFromIndexPath:toIndexPath:)]) {
[self.delegate tableView:self didMoveCellFromIndexPath:_selectedIndexPath toIndexPath:currentIndexPath];
}
_selectedIndexPath = currentIndexPath;
}
}
//
_tempView.center = CGPointMake(_tempView.center.x, point.y);
}
5.交換データとcellの位置異なる行高の交換時にcellが変形しないように、交換後すぐにreloadDataをリリースし、2つのcellをスクリーンショットして、スクリーンショットで交換のアニメーションをシミュレートする
/**
cell
*/
- (void)dh_updateDataSourceAndCellFromIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
if ([self numberOfSections] == 1) {
//
[_tempDataSource exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
// cell
[self moveRowAtIndexPath:fromIndexPath toIndexPath:toIndexPath];
}else {
//
id fromData = _tempDataSource[fromIndexPath.section][fromIndexPath.row];
id toData = _tempDataSource[toIndexPath.section][toIndexPath.row];
NSMutableArray *fromArray = [_tempDataSource[fromIndexPath.section] mutableCopy];
NSMutableArray *toArray = [_tempDataSource[toIndexPath.section] mutableCopy];
[fromArray replaceObjectAtIndex:fromIndexPath.row withObject:toData];
[toArray replaceObjectAtIndex:toIndexPath.row withObject:fromData];
[_tempDataSource replaceObjectAtIndex:fromIndexPath.section withObject:fromArray];
[_tempDataSource replaceObjectAtIndex:toIndexPath.section withObject:toArray];
// cell
[self beginUpdates];
[self moveRowAtIndexPath:fromIndexPath toIndexPath:toIndexPath];
[self moveRowAtIndexPath:toIndexPath toIndexPath:fromIndexPath];
[self endUpdates];
}
// reloadData
[self reloadData];
//
if (self.dataSource && [self.dataSource respondsToSelector:@selector(tableView:newDataSourceArrayAfterMove:)]) {
[self.dataSource tableView:self newDataSourceArrayAfterMove:_tempDataSource.copy];
}
// cell
UITableViewCell *cell = [self cellForRowAtIndexPath:fromIndexPath];
cell.hidden = NO;
UITableViewCell *cell1 = [self cellForRowAtIndexPath:toIndexPath];
cell1.hidden = YES;
UIView *tmpCell = [self dh_snapshotViewWithInputView:cell];
cell.hidden = YES;
tmpCell.frame = cell1.frame;
if (_toBottom == -1) {//
tmpCell.frame = CGRectMake(0, CGRectGetMinY(cell1.frame), cell.frame.size.width, cell.frame.size.height);
[self insertSubview:tmpCell belowSubview:_tempView];
}else if (_toBottom == 1) {//
tmpCell.frame = CGRectMake(0, CGRectGetMaxY(cell1.frame)-cell.frame.size.height, cell.frame.size.width, cell.frame.size.height);
[self insertSubview:tmpCell belowSubview:_tempView];
}else{
}
[UIView animateWithDuration:0.2 animations:^{
tmpCell.frame = cell.frame;
}completion:^(BOOL finished) {
cell.hidden = NO;
[tmpCell removeFromSuperview];
}];
}
6.エッジスクロール処理
- (void)dh_startEdgeScroll
{
_edgeScrollTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(dh_processEdgeScroll)];
[_edgeScrollTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
- (void)dh_processEdgeScroll
{
[self dh_gestureChanged:_gesture];
CGFloat minOffsetY = self.contentOffset.y + _edgeScrollRange;
CGFloat maxOffsetY = self.contentOffset.y + self.bounds.size.height - _edgeScrollRange;
CGPoint touchPoint = _tempView.center;
// tableView, , edgeScrollRange , tableView , tableView
if (touchPoint.y < _edgeScrollRange) {
if (self.contentOffset.y <= 0) {
return;
}else {
if (self.contentOffset.y - 1 < 0) {
return;
}
[self setContentOffset:CGPointMake(self.contentOffset.x, self.contentOffset.y - 1) animated:NO];
_tempView.center = CGPointMake(_tempView.center.x, _tempView.center.y - 1);
}
}
if (touchPoint.y > self.contentSize.height - _edgeScrollRange) {
if (self.contentOffset.y >= self.contentSize.height - self.bounds.size.height) {
return;
}else {
if (self.contentOffset.y + 1 > self.contentSize.height - self.bounds.size.height) {
return;
}
[self setContentOffset:CGPointMake(self.contentOffset.x, self.contentOffset.y + 1) animated:NO];
_tempView.center = CGPointMake(_tempView.center.x, _tempView.center.y + 1);
}
}
//
CGFloat maxMoveDistance = 20;
if (touchPoint.y < minOffsetY) {
//cell
CGFloat moveDistance = (minOffsetY - touchPoint.y)/_edgeScrollRange*maxMoveDistance;
[self setContentOffset:CGPointMake(self.contentOffset.x, self.contentOffset.y - moveDistance) animated:NO];
_tempView.center = CGPointMake(_tempView.center.x, _tempView.center.y - moveDistance);
}else if (touchPoint.y > maxOffsetY) {
//cell
CGFloat moveDistance = (touchPoint.y - maxOffsetY)/_edgeScrollRange*maxMoveDistance;
[self setContentOffset:CGPointMake(self.contentOffset.x, self.contentOffset.y + moveDistance) animated:NO];
_tempView.center = CGPointMake(_tempView.center.x, _tempView.center.y + moveDistance);
}
}