zoukankan      html  css  js  c++  java
  • iOS | 实现拖拽CollectionViewCell排序

    现在很多项目都会用到类似拖动的效果,比如今日头条和网易新闻之类的资讯类产品,都有用该技术设置模块顺序的操作。

    在iOS9.0之后,苹果提供相关的方法,非常方便。

    设定三个私有属性
    @property(nonatomic,strong) NSMutableArray *arr;
    
    @property(nonatomic,weak) UICollectionView *colView;
    
    @property(nonatomic,strong) UILongPressGestureRecognizer *longPress;
    //数据源
    - (NSMutableArray *)arr{
        
        if (!_arr) {
            _arr = [NSMutableArray arrayWithObjects:@(1),@(2),@(3),@(4),@(5),@(6),@(7),@(8),@(9), nil];
        }
        return _arr;
    }
    
    1. 先创建UICollectionView
    //创建布局对象
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //view
        UICollectionView *colView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    //背景色
        colView.backgroundColor = [UIColor whiteColor];
        colView.delegate = self;
        colView.dataSource = self;
    //控制布局
        colView.contentInset = UIEdgeInsetsMake(30, 20, 0, 20);
        CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
        CGFloat space = 20;
        NSInteger col = 3;
        CGFloat itemSize = (screenW - (( col + 1 ) * space) - 6) / 3;
        
        flowLayout.itemSize = CGSizeMake(itemSize, itemSize);
        flowLayout.minimumInteritemSpacing = space;
        flowLayout.minimumLineSpacing = space;
        //添加长按手势
        _longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressMoving:)];
        [colView addGestureRecognizer:_longPress];
        //属性连接
        self.colView = colView;
       //注册cell,记得先创建一个自定义cell
        [colView registerNib:[UINib nibWithNibName:@"MyCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
        [self.view addSubview:colView];
    
    --------------------------------------数据源方法---------------------------------------------
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        return self.arr.count;
    }
    
    // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
    - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        
        MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        
        cell.num.text = [NSString stringWithFormat:@"%@",self.arr[indexPath.row]];
        
        return cell;
        
    }
    

    2.长按手势响应事件

    - (void)longPressMoving:(UILongPressGestureRecognizer *)longPress{
    //    筛选长按手势状态
        switch (_longPress.state) {
    //            开始
            case UIGestureRecognizerStateBegan: {
                {
    //手势作用的位置
                    NSIndexPath *selectIndexPath = [self.colView indexPathForItemAtPoint:[_longPress locationInView:self.colView]];
                    // 找到当前的cell
                    MyCollectionViewCell *cell = (MyCollectionViewCell *)[self.colView cellForItemAtIndexPath:selectIndexPath];
    //                拽起变大动画效果
                    [UIView animateWithDuration:0.3 animations:^{
                        [cell setTransform:CGAffineTransformMakeScale(1.2, 1.2)];
                    }];
                   //开始移动
                    [_colView beginInteractiveMovementForItemAtIndexPath:selectIndexPath];
                }
                break;
            }
            case UIGestureRecognizerStateChanged: {
    //更新移动的位置
                [self.colView updateInteractiveMovementTargetPosition:[longPress locationInView:_longPress.view]];
                break;
            }
            case UIGestureRecognizerStateEnded: {
    //结束移动
                [self.colView endInteractiveMovement];
                break;
            }
            default: [self.colView cancelInteractiveMovement];
                break;
        }
    }
    

    3.实现苹果官方的代理方法

    - (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
        return YES;
    }
    
    - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
        
        NSLog(@"%zd---%zd",sourceIndexPath.row,destinationIndexPath.row);
        NSIndexPath *selectIndexPath = [self.colView indexPathForItemAtPoint:[_longPress locationInView:self.colView]];
       //交换数据源的内容
        [self.arr exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
    //    [self.colView reloadData];
        
        NSLog(@"%@",self.arr);
    }
    

    实现完以上的方法,可以快速构建一个可拖拽排序的cell界面。

  • 相关阅读:
    Ubuntu开机等待5分钟的取消方法
    329. 矩阵中的最长递增路径
    关于c语言中NULL的数值是否可以被修改
    #pragam在c++(visual studio 2019)编译器中的使用
    当cpu占有率过高时-sleep(0)的妙用
    inline解析
    一、【pytest实战--Web测试】搭建环境
    用openssl aes256 api实现文件加解密-带例程,兼容openssl enc -aes-256-cbc命令
    kali openvas安装
    C++关于变量初始化的琐记
  • 原文地址:https://www.cnblogs.com/JanChuJun/p/10102213.html
Copyright © 2011-2022 走看看