UICollectionView的item的長按移動(dòng)

點(diǎn)擊每一個(gè)item后移動(dòng)item改變item的位置。
實(shí)現(xiàn)這個(gè)功能有兩種方案
1.第一種使用蘋果給出的API直接調(diào)方法(IOS9以后)
2.第二種就是自己苦b的寫移動(dòng)前后的邏輯
通過查資料獲得的兩個(gè)方法代碼大致如下

第一種直接調(diào)API

增加長按手勢

//此處給其增加長按手勢,用此手勢觸發(fā)cell移動(dòng)效果
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];
    [_itemCollectionVIew addGestureRecognizer:longGesture];

手勢調(diào)用的方法

//ios9以后的系統(tǒng)(ios9API)
-(void)IOS9_action:(UILongPressGestureRecognizer *)longGesture{
    //判斷手勢狀態(tài)
    switch (longGesture.state) {
        case UIGestureRecognizerStateBegan:{
//增加的抖動(dòng)的動(dòng)畫
            [self shake];
            //判斷手勢落點(diǎn)位置是否在路徑上
            NSIndexPath *indexPath = [self.itemCollectionVIew indexPathForItemAtPoint:[longGesture locationInView:self.itemCollectionVIew]];
            if (indexPath == nil) {
                break;
            }
            //在路徑上則開始移動(dòng)該路徑上的cell
            [self.itemCollectionVIew beginInteractiveMovementForItemAtIndexPath:indexPath];
        }
            break;
        case UIGestureRecognizerStateChanged:
            //移動(dòng)過程當(dāng)中隨時(shí)更新cell位置
            [self.itemCollectionVIew updateInteractiveMovementTargetPosition:[longGesture locationInView:self.itemCollectionVIew]];
            break;
        case UIGestureRecognizerStateEnded:
            //移動(dòng)結(jié)束后關(guān)閉cell移動(dòng)
            [self.itemCollectionVIew endInteractiveMovement];
            break;
        default:
            [self.itemCollectionVIew cancelInteractiveMovement];
            break;
    }
}

系統(tǒng)的回調(diào)方法

//是否允許移動(dòng)item(ios9API)
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
    //返回YES允許其item移動(dòng)
    return YES;
}
//item移動(dòng)后改變的item的回調(diào)(ios9API)
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
//此處可以設(shè)置移動(dòng)后的數(shù)據(jù)源的數(shù)組中的位置
//也可以不寫,只是刷新后cell的狀態(tài)不是你所想要的
}

調(diào)換數(shù)據(jù)源數(shù)組中的位置,可以參考下面方法
//self.dataArray是數(shù)據(jù)源數(shù)組
//SelectedListModel是cell的model
//將數(shù)據(jù)源數(shù)組按照改變的item的位置重新組裝數(shù)組

-(void)compareMoveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath{
    NSInteger location = 0;
    NSInteger length = 0;
    if (sourceIndexPath.item > destinationIndexPath.item) {
        location = destinationIndexPath.item;
    }else{
        location = sourceIndexPath.item;
    }
    length = ABS(destinationIndexPath.item - sourceIndexPath.item)+1;
    NSRange rang = NSMakeRange(location , length);
    NSMutableIndexSet *set = [NSMutableIndexSet indexSet];
    [set addIndexesInRange:rang];
    NSMutableArray *array = [NSMutableArray arrayWithArray:[self.dataArray subarrayWithRange:rang]];
    [self.dataArray removeObjectsAtIndexes:set];
    if (location == sourceIndexPath.item) {
        SelectedListModel *model1 = array.firstObject;
        [array removeObjectAtIndex:0];
        [array addObject:model1];
        
    }else{
        SelectedListModel *model1 = array.lastObject;
        [array removeLastObject];
        [array insertObject:model1 atIndex:0];
    }
    [self.dataArray insertObjects:array atIndexes:set];
}

第二種是為了適配iOS9以前的系統(tǒng)

用到的屬性

/**之前選中cell的NSIndexPath*/
@property (nonatomic, strong) NSIndexPath *oldIndexPath;
/**單元格的截圖*/
@property (nonatomic, strong) UIView *snapshotView;
/**之前選中cell的NSIndexPath*/
@property (nonatomic, strong) NSIndexPath *moveIndexPath;

手勢實(shí)現(xiàn)的方法

//ios9以前的系統(tǒng)
-(void)action:(UILongPressGestureRecognizer *)longPress{
    switch (longPress.state) {
        case UIGestureRecognizerStateBegan:
        { // 手勢開始
            //判斷手勢落點(diǎn)位置是否在row上
            NSIndexPath *indexPath = [self.itemCollectionVIew indexPathForItemAtPoint:[longPress locationInView:self.itemCollectionVIew]];
            self.oldIndexPath = indexPath;
            if (indexPath == nil) {
                break;
            }
            UICollectionViewCell *cell = [self.itemCollectionVIew cellForItemAtIndexPath:indexPath];
            // 使用系統(tǒng)的截圖功能,得到cell的截圖視圖
            UIView *snapshotView = [cell snapshotViewAfterScreenUpdates:NO];
            snapshotView.layer.cornerRadius = 3;
            snapshotView.layer.masksToBounds = YES;
            snapshotView.frame = cell.frame;
            [self.itemCollectionVIew addSubview:self.snapshotView = snapshotView];
            // 截圖后隱藏當(dāng)前cell
            cell.hidden = YES;
            
            CGPoint currentPoint = [longPress locationInView:self.itemCollectionVIew];
            [UIView animateWithDuration:0.25 animations:^{
                snapshotView.transform = CGAffineTransformMakeScale(1.05, 1.05);
                snapshotView.center = currentPoint;
            }];
        }
            break;
        case UIGestureRecognizerStateChanged:
        { // 手勢改變
            //當(dāng)前手指位置 截圖視圖位置隨著手指移動(dòng)而移動(dòng)
            CGPoint currentPoint = [longPress locationInView:self.itemCollectionVIew];
            self.snapshotView.center = currentPoint;
            // 計(jì)算截圖視圖和哪個(gè)可見cell相交
            for (UICollectionViewCell *cell in self.itemCollectionVIew.visibleCells) {
                // 當(dāng)前隱藏的cell就不需要交換了,直接continue
                if ([self.itemCollectionVIew indexPathForCell:cell] == self.oldIndexPath) {
                    continue;
                }
                // 計(jì)算中心距
                CGFloat space = sqrtf(pow(self.snapshotView.center.x - cell.center.x, 2) + powf(self.snapshotView.center.y - cell.center.y, 2));
                if (space <= self.snapshotView.bounds.size.width / 2) {
                    self.moveIndexPath = [self.itemCollectionVIew indexPathForCell:cell];
                    //移動(dòng) 會(huì)調(diào)用willMoveToIndexPath方法更新數(shù)據(jù)源
                    [self.itemCollectionVIew moveItemAtIndexPath:self.oldIndexPath toIndexPath:self.moveIndexPath];
//上面提到的方法,將數(shù)據(jù)源數(shù)組按照改變的item的位置重新組裝數(shù)組
                    [self compareMoveItemAtIndexPath:_oldIndexPath toIndexPath:_moveIndexPath];
                    //設(shè)置移動(dòng)后的起始indexPath
                    self.oldIndexPath = self.moveIndexPath;
                    break;
                }
            }
        }
            break;
        default:
        { // 手勢結(jié)束和其他狀態(tài)
            UICollectionViewCell *cell = [self.itemCollectionVIew cellForItemAtIndexPath:self.oldIndexPath];
            // 結(jié)束動(dòng)畫過程中停止交互,防止出問題
            self.itemCollectionVIew.userInteractionEnabled = NO;
            // 給截圖視圖一個(gè)動(dòng)畫移動(dòng)到隱藏cell的新位置
            [UIView animateWithDuration:0.25 animations:^{
                self.snapshotView.center = cell.center;
                self.snapshotView.transform = CGAffineTransformMakeScale(1.0, 1.0);
            } completion:^(BOOL finished) {
                // 移除截圖視圖,顯示隱藏的cell并開始交互
                [self.snapshotView removeFromSuperview];
                cell.hidden = NO;
                self.itemCollectionVIew.userInteractionEnabled = YES;
            }];
        }
            break;
    }
}

根據(jù)需要調(diào)用

- (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {
    //判斷是否是ios以前的系統(tǒng)
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0){
        [self action:longGesture];
    }else{
       [self IOS9_action:longGesture];
    }
}

注釋寫的很清楚,理解應(yīng)該不是很難。。。

最后編輯于
?著作權(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),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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