期望效果
1.長按即可觸發移動cell,操作邏輯簡單;
2.移動cell時越靠近屏幕邊緣,速度越快;
3.被移動cell的樣式可以自定義;
github地址
JXMovableCellTableView,如果你喜歡,不妨給顆星吧_~
調研
如果只是實現移動UITableViewCell,系統自帶的API即可搞定。
調用下面的方法[tableView setEditing:YES animated:YES];
即可進入編輯模式。然后實現下面的方法即可開啟移動cell。
//默認編輯模式下,每個cell左邊有個紅色的刪除按鈕,設置為None即可去掉
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
//是否允許indexPath的cell移動
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//更新數據源
}
用系統的方法有幾個缺點:
1.需要用一個開關去控制編輯狀態,不方便;
2.移動cell的時候cell右邊有個指示圖標,看著不爽;
3.被移動cell的樣式不能自己定制。
綜上所述,需要自己去寫效果了。
實現原理
大概原理:為UITableView添加一個長按手勢,然后給選中的cell截圖;讓截圖隨著手勢移動,同時記錄選中的indexPath,方便位置互換。
1.添加長按手勢
- (void)jx_addGesture
{
_gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(jx_processGesture:)];
_gesture.minimumPressDuration = _gestureMinimumPressDuration;
[self addGestureRecognizer:_gesture];
}
2.處理手勢開始,需要通過dataSource方法獲取數據源
- (void)jx_gestureBegan:(UILongPressGestureRecognizer *)gesture
{
CGPoint point = [gesture locationInView:gesture.view];
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 jx_startEdgeScroll];
}
//每次移動開始獲取一次數據源
if (self.dataSource && [self.dataSource respondsToSelector:@selector(dataSourceArrayInTableView:)]) {
_tempDataSource = [self.dataSource dataSourceArrayInTableView:self].mutableCopy;
}
_selectedIndexPath = selectedIndexPath;
UITableViewCell *cell = [self cellForRowAtIndexPath:selectedIndexPath];
_tempView = [self jx_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:kJXMovableCellAnimationTime animations:^{
_tempView.center = CGPointMake(_tempView.center.x, point.y);
}];
}
3.處理手勢變化,移動成功之后用delegate告訴使用者
- (void)jx_gestureChanged:(UILongPressGestureRecognizer *)gesture
{
CGPoint point = [gesture locationInView:gesture.view];
NSIndexPath *currentIndexPath = [self indexPathForRowAtPoint:point];
if (currentIndexPath && ![_selectedIndexPath isEqual:currentIndexPath]) {
//交換數據源和cell
[self jx_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);
}
交換數據源和cell
- (void)jx_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];
}
}
4.處理手勢結束或取消,結束之后通過dataSource代理返回交換后的數據源。
- (void)jx_gestureEndedOrCancelled:(UILongPressGestureRecognizer *)gesture
{
if (_canEdgeScroll) {
[self jx_stopEdgeScroll];
}
//返回交換后的數據源
if (self.dataSource && [self.dataSource respondsToSelector:@selector(tableView:newDataSourceArrayAfterMove:)]) {
[self.dataSource tableView:self newDataSourceArrayAfterMove:_tempDataSource.copy];
}
if (self.delegate && [self.delegate respondsToSelector:@selector(tableView:endMoveCellAtIndexPath:)]) {
[self.delegate tableView:self endMoveCellAtIndexPath:_selectedIndexPath];
}
UITableViewCell *cell = [self cellForRowAtIndexPath:_selectedIndexPath];
[UIView animateWithDuration:kJXMovableCellAnimationTime animations:^{
_tempView.frame = cell.frame;
} completion:^(BOOL finished) {
cell.hidden = NO;
[_tempView removeFromSuperview];
_tempView = nil;
}];
}
5.開啟邊緣滾動
用CADisplayLink
實現,1/60秒刷新一次,和屏幕刷新頻率一樣。
- (void)jx_startEdgeScroll
{
_edgeScrollTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(jx_processEdgeScroll)];
[_edgeScrollTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
6.處理邊緣滾動
- (void)jx_processEdgeScroll
{
[self jx_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在往上移動, moveDistance越大移動越快
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在往下移動, moveDistance越大移動越快
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);
}
}
使用
直接繼承JXMovableCellTableView
就可以使用了,然后通過公開屬性進行自定義:
/**
* 長按手勢最小觸發時間,默認1.0,最小0.2
*/
@property (nonatomic, assign) CGFloat gestureMinimumPressDuration;
/**
* 自定義可移動cell的截圖樣式
*/
@property (nonatomic, copy) void(^drawMovalbeCellBlock)(UIView *movableCell);
/**
* 是否允許拖動到屏幕邊緣后,開啟邊緣滾動,默認YES
*/
@property (nonatomic, assign) BOOL canEdgeScroll;
/**
* 邊緣滾動觸發范圍,默認150,越靠近邊緣速度越快
*/
@property (nonatomic, assign) CGFloat edgeScrollRange;
總結
通過截圖大法實現以假亂真,自由移動截圖產生的"cell";交換的時候,首先要交換數據源,再交換cell;處理邊緣滾動的時候,需要小心各種邊界條件。
既然UITableViewCell可以自由移動了,那么UICollectionViewCell同理也可以實現。下篇文章就講可移動UICollectionViewCell的實現。