2023-04-03 CollectionView 长按排序 系

2023-04-02  本文已影响0人  cc帅气的昵称

// 长按手势

    UILongPressGestureRecognizer * listLongPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(listLongPressAction:)];

    [self.collectionViewaddGestureRecognizer:listLongPress];

- (void)listLongPressAction:(UIGestureRecognizer*)gesture

{

    UIGestureRecognizerState state = gesture.state;

    CGPointlocation = [gesturelocationInView:self.collectionView];

    switch(state) {

        ///手势开始

        case UIGestureRecognizerStateBegan:

        {

            /* 判断手势落点位置是否在路径上 */

            NSIndexPath  *indexPath  = [self.collectionViewindexPathForItemAtPoint:location];

            if(indexPath ==nil) {

                break;

            }

            /* 在路径上则开始移动该路径上的cell */

            [self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];

            FileScanGridCell  *nCell = (FileScanGridCell*)[self.collectionView cellForItemAtIndexPath:indexPath];

            [UIView animateWithDuration:0.35 animations:^{

                nCell.alpha=0.6;

            }];

            break;

        }

            ///手势变化

        case UIGestureRecognizerStateChanged:

        {

            /* 移动过程当中随时更新cell位置 */

            [self.collectionView updateInteractiveMovementTargetPosition:location];

            break;

        }

        case UIGestureRecognizerStateEnded:

        {

            /* 移动结束后关闭cell移动 */

            [self.collectionView endInteractiveMovement];

            NSIndexPath  *indexPath  = [self.collectionViewindexPathForItemAtPoint:location];

            if(indexPath ==nil) {

                break;

            }

            FileScanGridCell  *nCell = (FileScanGridCell*)[self.collectionView cellForItemAtIndexPath:indexPath];

            [UIView animateWithDuration:0.35 animations:^{

                nCell.alpha=1;

            }];

            break;

        }

        ///手势结束

        default:

        {

            [self.collectionView cancelInteractiveMovement];

            NSIndexPath  *indexPath  = [self.collectionViewindexPathForItemAtPoint:location];

            if(indexPath ==nil) {

                break;

            }

            FileScanGridCell  *nCell = (FileScanGridCell*)[self.collectionView cellForItemAtIndexPath:indexPath];

            [UIView animateWithDuration:0.35 animations:^{

                nCell.alpha=1;

            }];

            break;

        }

    }

}

//允许移动的item

- (BOOL)collectionView:(UICollectionView*)collectionViewcanMoveItemAtIndexPath:(NSIndexPath*)indexPath {

    return YES;

}

//对交换过的item数据进行操作

- (void)collectionView:(UICollectionView*)collectionViewmoveItemAtIndexPath:(NSIndexPath*)sourceIndexPathtoIndexPath:(NSIndexPath*)destinationIndexPath {

    if(sourceIndexPath.row== destinationIndexPath.row) {

        return;

    }elseif(sourceIndexPath.row> destinationIndexPath.row) {

        LYFileInfoModel*model =self.dataArray[sourceIndexPath.row];

        [self.dataArrayremoveObjectAtIndex:sourceIndexPath.row];

        [self.dataArrayinsertObject:modelatIndex:destinationIndexPath.row];

    }elseif(sourceIndexPath.row< destinationIndexPath.row) {

        LYFileInfoModel*model =self.dataArray[sourceIndexPath.row];

        [self.dataArrayinsertObject:modelatIndex:destinationIndexPath.row+1];

        [self.dataArrayremoveObjectAtIndex:sourceIndexPath.row];

    }

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        // 需要延迟执行的代码

        [self.collectionView reloadData];

    });

}

//抑制item移动 指定位置禁止交换

- (NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveOfItemFromOriginalIndexPath:(NSIndexPath *)originalIndexPath atCurrentIndexPath:(NSIndexPath *)currentIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath {

    returnproposedIndexPath;

}

上一篇 下一篇

猜你喜欢

热点阅读