iOS9之前collectionView拖拽重排的實(shí)現(xiàn)

最近因需要實(shí)現(xiàn)一個(gè)標(biāo)簽類的 demo, 需要支持拖拽重排, 首先想到的便是 collectionView, 并且很快就在 API 文檔中找到了類似的接口:

- (BOOL)beginInteractiveMovementForItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
- (void)updateInteractiveMovementTargetPosition:(CGPoint)targetPosition NS_AVAILABLE_IOS(9_0);
- (void)endInteractiveMovement NS_AVAILABLE_IOS(9_0);
- (void)cancelInteractiveMovement NS_AVAILABLE_IOS(9_0);

用系統(tǒng)提供的這幾個(gè)接口很快便能實(shí)現(xiàn)我們的需求, 代碼見 gitbub地址.
但是你有沒有發(fā)現(xiàn)上面的方法后面跟著的NS_AVAILABLE_IOS(9_0), iOS9才開始支持, 這就尷尬了.
那iOS9之前怎么手動(dòng)實(shí)現(xiàn)這個(gè)需求呢, 于是邊分析邊開始列出實(shí)現(xiàn)步驟:

  1. 長(zhǎng)按 cell 進(jìn)入拖拽模式
  2. 隱藏當(dāng)前 cell, 并添加一個(gè)當(dāng)前 cell 的截圖到 collectionView 上
  3. 隨手勢(shì)移動(dòng)截圖, 當(dāng)截圖的center 進(jìn)入到另一個(gè) cell2 的范圍內(nèi)的時(shí)候, 將隱藏的 cell 移動(dòng)到cell2的位置, 并移動(dòng) cell2及cell2之后的 cell
  4. 若繼續(xù)移動(dòng), 則執(zhí)行(3)
  5. 若松開手指結(jié)束移動(dòng), 則移除掉截圖, 并顯示之前隱藏的 cell

過(guò)程理順了, 那么接下來(lái)便是代碼的實(shí)現(xiàn)了.

1. 長(zhǎng)按 cell 進(jìn)入拖拽模式

我這里直接給 collectionView添加了一個(gè)長(zhǎng)按手勢(shì)

UILongPressGestureRecognizer *longG = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
longG.minimumPressDuration = 0.2f;
[self.collectionView addGestureRecognizer:longG];

- (void)longPress:(UILongPressGestureRecognizer *)lpGesture
{
    switch(lpGesture.state) {
        case UIGestureRecognizerStateBegan:
        {
            [self collectionViewCellDidBeginChange:lpGesture];
            break;
        }
        case UIGestureRecognizerStateChanged:
        {
            [self collectionViewCellDidChange:lpGesture];
            break;
        }
        case UIGestureRecognizerStateEnded:
        {
            [self collectionViewCellDidEndChange:lpGesture];
            break;
        }
        default:
            if (AVAILABLE_IOS_9_0)
            {
                [self.collectionView cancelInteractiveMovement];
            }
            break;
    }
}
2. 隱藏當(dāng)前 cell, 并添加一個(gè)當(dāng)前 cell 的截圖到 collectionView 上
- (void)collectionViewCellDidBeginChange:(UILongPressGestureRecognizer *)lpGesture
{
    self.beginIndexPath = [self.collectionView indexPathForItemAtPoint:[lpGesture locationInView:self.collectionView]];
    self.beginCell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:self.beginIndexPath];
    
    if (AVAILABLE_IOS_9_0)
    {
        [self.collectionView beginInteractiveMovementForItemAtIndexPath:self.beginIndexPath];
    }
    else
    {
        // 截圖  并隱藏原 cell
        self.tempView = [self.beginCell snapshotViewAfterScreenUpdates:YES];
        self.tempView.frame = self.beginCell.frame;
        [self.collectionView addSubview:self.tempView];
        
        self.beginPoint = [lpGesture locationInView:self.collectionView];
        self.beginCell.hidden = YES;
    }
}

3. 移動(dòng)截圖
- (void)collectionViewCellDidChange:(UILongPressGestureRecognizer *)lpGesture
{
    CGPoint targetPosion = [lpGesture locationInView:lpGesture.view];
    
    if (AVAILABLE_IOS_9_0)
    {
        [self.collectionView updateInteractiveMovementTargetPosition:targetPosion];
    }
    else
    {
        CGFloat tranX = [lpGesture locationOfTouch:0 inView:self.collectionView].x - self.beginPoint.x;
        CGFloat tranY = [lpGesture locationOfTouch:0 inView:self.collectionView].y - self.beginPoint.y;
        
        // 設(shè)置截圖視圖位置
        self.tempView.center = CGPointApplyAffineTransform(self.tempView.center, CGAffineTransformMakeTranslation(tranX, tranY));
        self.beginPoint = [lpGesture locationOfTouch:0 inView:_collectionView];
        
        // 計(jì)算截圖視圖和哪個(gè)cell相交
        for (UICollectionViewCell *cell in [_collectionView visibleCells])
        {
            //跳過(guò)隱藏的cell
            if ([_collectionView indexPathForCell:cell] == self.beginIndexPath)
            {
                continue;
            }
            //計(jì)算中心距
            CGFloat space = sqrtf(pow(self.tempView.center.x - cell.center.x, 2) + powf(self.tempView.center.y - cell.center.y, 2));
            
            //如果相交一半且兩個(gè)視圖Y的絕對(duì)值小于高度的一半就移動(dòng)
            if (space <= self.tempView.bounds.size.width * 0.5 && (fabs(self.tempView.center.y - cell.center.y) <= self.tempView.bounds.size.height * 0.5))
            {
                NSIndexPath *nextIndexPath = [_collectionView indexPathForCell:cell];
                
                [self updateDataWithSourceIndexPath:self.beginIndexPath toIndexPath:nextIndexPath];
                
                [_collectionView moveItemAtIndexPath:self.beginIndexPath toIndexPath:nextIndexPath];
                
                //設(shè)置移動(dòng)后的起始indexPath
                self.beginIndexPath = nextIndexPath;
                
                break;
            }
        }
    }
}

數(shù)據(jù)源的改動(dòng)

- (void)updateDataWithSourceIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    if (sourceIndexPath.section == destinationIndexPath.section) // 同一個(gè) section 間移動(dòng)
    {
        if (destinationIndexPath.item > sourceIndexPath.item) {
            for (NSUInteger i = sourceIndexPath.item; i < destinationIndexPath.item ; i ++) {
                [self.dataArray[sourceIndexPath.section] exchangeObjectAtIndex:i withObjectAtIndex:i + 1];
            }
        }else{
            for (NSUInteger i = sourceIndexPath.item; i > destinationIndexPath.item ; i --) {
                [self.dataArray[sourceIndexPath.section] exchangeObjectAtIndex:i withObjectAtIndex:i - 1];
            }
        }
    }
    else // 不同 section 之間移動(dòng) ,  刪除 sourceData, 插入到 destinationData
    {
        NSMutableArray *sourceArr = self.dataArray[sourceIndexPath.section];
        NSMutableArray *destinationArr = self.dataArray[destinationIndexPath.section];
        
        id obj = [sourceArr objectAtIndex:sourceIndexPath.row];
        
        [sourceArr removeObjectAtIndex:sourceIndexPath.row];
        
        [destinationArr insertObject:obj atIndex:destinationIndexPath.row];
    }
    
}
4. 若繼續(xù)移動(dòng), 則執(zhí)行(3)
5. 若松開手指結(jié)束移動(dòng), 則移除掉截圖, 并顯示之前隱藏的 cell
- (void)collectionViewCellDidEndChange:(UILongPressGestureRecognizer *)lpGesture
{
    if (AVAILABLE_IOS_9_0)
    {
        [self.collectionView endInteractiveMovement];
    }
    else
    {
        [UIView animateWithDuration:0.25 animations:^{
            
            self.tempView.center = self.beginCell.center;
            
        }completion:^(BOOL finished) {
            
            [self.tempView removeFromSuperview];
            self.beginCell.hidden = NO;
        }];
    }
}

好了, 到此就完成了 iOS9以下的適配, 實(shí)現(xiàn)起來(lái)也不是很復(fù)雜, 主要的是實(shí)現(xiàn)之前把思路理順, 那實(shí)現(xiàn)起來(lái)就不難了.

詳細(xì)的代碼見gitbub地址.如果有收獲,歡迎 star!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容