系统的UICollectionView和UITableView有提供在编辑态拖拽排序的功能,但是需要指定的EditStyle,而且还只能拖动排序按钮区域,大部分时候效果很难满足业务需求,因此我们需要定制可拖拽Cell的SDMovableCellCollectionView。文章最后附有Demo的Git地址,可查看完整代码。
- (void)gestureBegan:(UILongPressGestureRecognizer *)gesture {CGPoint point = [gesture locationInView:gesture.view];NSIndexPath *selectedIndexPath = [self indexPathForItemAtPoint:point];if (!selectedIndexPath) {return;}UICollectionViewCell *cell = [self cellForItemAtIndexPath:selectedIndexPath];cell.alpha = 1.0;if (self.dataSource && [self.dataSource respondsToSelector:@selector(collectionView:canMoveItemAtIndexPath:)]) {if (![self.dataSource collectionView:self canMoveItemAtIndexPath:selectedIndexPath]) {//It is not allowed to move the cell, then shake it to prompt the user.if (self.canHintWhenCannotMove) {CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];shakeAnimation.duration = 0.25;shakeAnimation.values = @[@(-20), @(20), @(-10), @(10), @(0)];[cell.layer addAnimation:shakeAnimation forKey:@"shake"];}if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:tryMoveUnmovableCellAtIndexPath:)]) {[self.delegate collectionView:self tryMoveUnmovableCellAtIndexPath:selectedIndexPath];}UICollectionViewCell *cell = [self cellForItemAtIndexPath:selectedIndexPath];cell.alpha = 0.7;return;}}if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:willMoveCellAtIndexPath:)]) {[self.delegate collectionView:self willMoveCellAtIndexPath:selectedIndexPath];}if (_canEdgeScroll) {[self startEdgeScroll];}//Get a data source every time you moveif (self.dataSource && [self.dataSource respondsToSelector:@selector(dataSourceArrayInCollectionView:)]) {_tempDataSource = [self.dataSource dataSourceArrayInCollectionView:self];}_selectedIndexPath = selectedIndexPath;_startIndexPath = selectedIndexPath;if (self.canFeedback) {[self.generator prepare];[self.generator impactOccurred];}if (self.dataSource && [self.dataSource respondsToSelector:@selector(snapshotViewWithCell:)]) {UIView *snapView = [self.dataSource snapshotViewWithCell:cell];_snapshot = [self snapshotViewWithInputView:snapView];} else {_snapshot = [self snapshotViewWithInputView:cell];}if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:customizeMovalbeCell:)]) {[self.delegate collectionView:self customizeMovalbeCell:_snapshot];} else {_snapshot.layer.shadowColor = [UIColor grayColor].CGColor;_snapshot.layer.masksToBounds = NO;_snapshot.layer.cornerRadius = 0;_snapshot.layer.shadowOffset = CGSizeMake(-5, 0);_snapshot.layer.shadowOpacity = 0.4;_snapshot.layer.shadowRadius = 5;}// _snapshot.frame = CGRectMake((cell.frame.size.width - _snapshot.frame.size.width)/2.0f, cell.frame.origin.y + (cell.frame.size.height - _snapshot.frame.size.height)/2.0, _snapshot.frame.size.width, _snapshot.frame.size.height);_snapshot.frame = cell.frame;[self addSubview:_snapshot];// 记录手势中心偏移self.gesturePointOffsetX = point.x - _snapshot.center.x;self.gesturePointOffsetY = point.y - _snapshot.center.y;cell.hidden = YES;if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:customizeStartMovingAnimation:fingerPoint:)]) {[self.delegate collectionView:self customizeStartMovingAnimation:_snapshot fingerPoint:point];} else {[UIView animateWithDuration:0.3 animations:^{self.snapshot.transform = CGAffineTransformScale(self.snapshot.transform, 1.1, 1.1);}];}
}- (void)gestureChanged:(UILongPressGestureRecognizer *)gesture {CGPoint point = [gesture locationInView:gesture.view];point.x -= self.gesturePointOffsetX;point.y -= self.gesturePointOffsetY;point = CGPointMake([self limitSnapshotCenterX:point.x], [self limitSnapshotCenterY:point.y]);//point = CGPointMake(_snapshot.center.x, [self limitSnapshotCenterY:point.y]);//Let the screenshot follow the gesture_snapshot.center = point;NSIndexPath *currentIndexPath = [self indexPathForItemAtPoint:point];if (!currentIndexPath) {return;}UICollectionViewCell *selectedCell = [self cellForItemAtIndexPath:_selectedIndexPath];selectedCell.hidden = YES;if (self.dataSource && [self.dataSource respondsToSelector:@selector(collectionView:canMoveItemAtIndexPath:)]) {if (![self.dataSource collectionView:self canMoveItemAtIndexPath:currentIndexPath]) {return;}}if (currentIndexPath && ![_selectedIndexPath isEqual:currentIndexPath]) {//Exchange data source and cell[self updateDataSourceAndCellFromIndexPath:_selectedIndexPath toIndexPath:currentIndexPath];if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:didMoveCellFromIndexPath:toIndexPath:)]) {[self.delegate collectionView:self didMoveCellFromIndexPath:_selectedIndexPath toIndexPath:currentIndexPath];}_selectedIndexPath = currentIndexPath;}
}- (void)gestureEndedOrCancelled:(UILongPressGestureRecognizer *)gesture {if (_canEdgeScroll) {[self stopEdgeScroll];}if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:endMoveCellAtIndexPath:)]) {[self.delegate collectionView:self endMoveCellAtIndexPath:_selectedIndexPath];}UICollectionViewCell *cell = [self cellForItemAtIndexPath:_selectedIndexPath];cell.alpha = 0.7;[UIView animateWithDuration:0.3 animations:^{self.snapshot.transform = CGAffineTransformIdentity;self.snapshot.frame = cell.frame;
// self.snapshot.frame = CGRectMake((cell.frame.size.width - self.snapshot.frame.size.width)/2.0f, cell.frame.origin.y + (cell.frame.size.height - self.snapshot.frame.size.height)/2.0, self.snapshot.frame.size.width, self.snapshot.frame.size.height);} completion:^(BOOL finished) {cell.hidden = NO;[self.snapshot removeFromSuperview];self.snapshot = nil;}];// 延时后更新整个数据源dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{[self reloadData];});
}
SDMovableCellCollectionView