版权声明:本文为博主原创文章,转载请附上本文链接地址。from : https://blog.csdn.net/lovechris00 https://blog.csdn.net/lovechris00/article/details/52043532
iOS拖拽移动UICollectionView下载地址:http://download.csdn.net/detail/lovechris00/9587439
这个月项目用到了拖拽移动UICollectionView的位置,
实现效果:
原理如下:
扫描二维码关注公众号,回复:
5839565 查看本文章
拖拽方法
1、获取当前手势位置,及对应cell的indexPath
- (void)longPressGestureRecognized:(id)sender{
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
UIGestureRecognizerState longPressState = longPress.state;
//手指在collectionView中的位置
_fingerLocation = [longPress locationInView:self];
//手指按住位置对应的indexPath,可能为nil
_relocatedIndexPath = [self indexPathForItemAtPoint:_fingerLocation];
开始拖拽
1、设置开始时选中的Cell的位置(indexPath)为初始位置;
//手势开始,对被选中cell截图,隐藏原cell
_originalIndexPath = [self indexPathForItemAtPoint:_fingerLocation];
2、对被选中cell截图,隐藏原cell
originalIndexPath可能为空,需要非空判断
if (_originalIndexPath) {
[self cellSelectedAtIndexPath:_originalIndexPath];
}
/**
* cell被长按手指选中,对其进行截图,原cell隐藏
*/
- (void)cellSelectedAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [self cellForItemAtIndexPath: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;
}];
}
#pragma mark - 返回一个给定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;
}
拖拽过程中
1、截图跟随手指移动
//截图跟随手指移动
CGPoint center = _snapshot.center;
center.y = _fingerLocation.y;
center.x = _fingerLocation.x ;
_snapshot.center = center;
2、判断是否到达整个collectionView的边缘
如果到了边缘,就
判断逻辑,该截图的最小Y大于collectionView的最大Y ,就向上滚动
最大Y值小于collectionView的最小Y,就向下滚动
if ([self checkIfSnapshotMeetsEdge]) {
[self startAutoScrollTimer];
}else{
[self stopAutoScrollTimer];
}
#pragma mark - 检查截图是否到达整个collectionView的边缘,并作出响应
- (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;
}
#pragma mark - timer methods
/**
* 创建定时器并运行
*/
- (void)startAutoScrollTimer{
if (!_autoScrollTimer) {
_autoScrollTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(startAutoScroll)];
[_autoScrollTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
}
/**
* 停止定时器并销毁
*/
- (void)stopAutoScrollTimer{
if (_autoScrollTimer) {
[_autoScrollTimer invalidate];
_autoScrollTimer = nil;
}
}
#pragma mark - 开始自动滚动
- (void)startAutoScroll{
CGFloat pixelSpeed = 4;
if (_autoScrollDirection == RTSnapshotMeetsEdgeTop) {//向下滚动
if (self.contentOffset.y > 0) {//向下滚动最大范围限制
[self setContentOffset:CGPointMake(0, self.contentOffset.y - pixelSpeed)];
_snapshot.center = CGPointMake(_snapshot.center.x, _snapshot.center.y - pixelSpeed);
}
}else{ //向上滚动
if (self.contentOffset.y + self.bounds.size.height < self.contentSize.height) {//向下滚动最大范围限制
[self setContentOffset:CGPointMake(0, self.contentOffset.y + pixelSpeed)];
_snapshot.center = CGPointMake(_snapshot.center.x, _snapshot.center.y + pixelSpeed);
}
}
/* 当把截图拖动到边缘,开始自动滚动,如果这时手指完全不动,则不会触发‘UIGestureRecognizerStateChanged’,对应的代码就不会执行,导致虽然截图在tableView中的位置变了,但并没有移动那个隐藏的cell,用下面代码可解决此问题,cell会随着截图的移动而移动
*/
// _relocatedIndexPath = [self indexPathForRowAtPoint:_snapshot.center];
_relocatedIndexPath = [self indexPathForItemAtPoint:_snapshot.center] ;
if (_relocatedIndexPath && ![_relocatedIndexPath isEqual:_originalIndexPath]) {
[self cellRelocatedToNewIndexPath:_relocatedIndexPath];
}
}
3、判断是否进入其他cell的范围
if (_relocatedIndexPath && ![_relocatedIndexPath isEqual:_originalIndexPath]) {
[self cellRelocatedToNewIndexPath:_relocatedIndexPath];
NSLog(@"elocated Success");
}
如果到达了
1)就获取数据并更新数据源
2)把当前手势的位置作为初始位置;
3)把数据通过代理传出去;
- (void)cellRelocatedToNewIndexPath:(NSIndexPath *)indexPath{
//更新数据源并返回给外部
[self updateDataSource];
//交换移动cell位置
[self moveItemAtIndexPath:_originalIndexPath toIndexPath:indexPath];
//更新cell的原始indexPath为当前indexPath
_originalIndexPath = indexPath;
}
# pragma mark - Private methods
/**修改数据源,通知外部更新数据源*/
- (void)updateDataSource{
//通过DataSource代理获得原始数据源数组
NSMutableArray *tempArray = [NSMutableArray array];
if ([self.adataSource respondsToSelector:@selector(originalArrayDataForcollectionView:)]) {
[tempArray addObjectsFromArray:[self.adataSource originalArrayDataForcollectionView:self]];
}
//移动数据
[self moveObjectInMutableArray:tempArray fromIndex:_originalIndexPath.row toIndex:_relocatedIndexPath.row];
// 将新数组传出外部以更改数据源
if ([self.adelegate respondsToSelector:@selector(collectionView:newArrayDataForDataSource:)]) {
[self.adelegate collectionView:self newArrayDataForDataSource:tempArray];
}
}
/**
* 将可变数组中的一个对象移动到该数组中的另外一个位置
* @param array 要变动的数组
* @param fromIndex 从这个index
* @param toIndex 移至这个index
*/
- (void)moveObjectInMutableArray:(NSMutableArray *)array fromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex{
NSInteger tempIndex = 0;
NSMutableString *tempTitle = nil;
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];
}
}
}
拖拽结束
1、停止动画;
[self stopAutoScrollTimer];
[self didEndDraging];
if ([self.adelegate respondsToSelector:@selector(cellDidEndMovingIncollectionView:)]) {
[self.adelegate cellDidEndMovingIncollectionView:self];
}
2、显示cell,移除截图
/**
* 拖拽结束,显示cell,并移除截图
*/
- (void)didEndDraging{
UICollectionViewCell *cell = [self cellForItemAtIndexPath:_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;
}];
}
iOS拖拽移动UICollectionView下载地址:http://download.csdn.net/detail/lovechris00/9587439