iOS collectionView拖拽排序

项目中首页按钮按照需求需要实现拖拽排序并且记录排序后的布局,下次再进入APP后展示排序后的布局。功能分析实现此功能需要实现两个点,第一就是拖拽排序的实现,第二就是存储排序后的布局,针对第一个功能点,拖拽排序:这个可以使用collectionView系统自带的功能来实现,针对第二个功能点,我使用NSUserDefaults本地存储盛放collectionViewCell内容的数组...
摘要由CSDN通过智能技术生成

项目中首页按钮按照需求需要实现拖拽排序并且记录排序后的布局,下次再进入APP后展示排序后的布局。

功能分析

实现此功能需要实现两个点,第一就是拖拽排序的实现,第二就是存储排序后的布局,针对第一个功能点,拖拽排序:这个可以使用collectionView系统自带的功能来实现,针对第二个功能点,我使用NSUserDefaults本地存储盛放collectionViewCell内容的数组。
整体效果如图:

668737-53f53f5e2a4bb9a7.png
屏幕快照 2019-01-29 上午11.10.24.png

本地存储的实现

每个cell由背景图片和title组成,NSDictionary *dic1 = @{@"title":@"标题", @"img":@"bgImg"};每个字典存放两个字段,根据需求,创建相应的字典,NSArray *tempArr = @[dic1,dic2,dic3,dic4,dic5]``[self.homeBtnsArr addObjectsFromArray:tempArr];将字典放在数组中,便于在collectionView的代理方法中使用,[[NSUserDefaults standardUserDefaults] setObject:self.homeBtnsArr forKey:@"stuHomeBtns"];将数组做本地储存,在每次进入界面时,先去NSUserDefaults中查找存放的数组,再去使用。

拖拽移动功能的实现

1、创建collectionView

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH-50.0)/4, 85);
    flowLayout.minimumLineSpacing = 0.1;
    flowLayout.minimumInteritemSpacing = 0.1;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    self.collectionView = collectionView;
    [self addSubview:collectionView];
    [collectionView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(0);
    }];
    [collectionView registerClass:[HomeSortBtnCell class] forCellWithReuseIdentifier:@"HomeSortBtnCell"];

2、在collectionView上添加长按手势

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];
    [collectionView addGestureRecognizer:longPress];

3、长按手势响应方法

#pragma mark 长按响应方法
- (void)handlelongGesture:(UILongPressGestureRecognizer *)longPress {
    [self action:longPress];
}

4、处理长按手势

- (void)action:longPress:(UILongPressGestureRecognizer *)longPress {
    switch (longPress.state) {
        case UIGestureRecognizerStateBegan:
            {
                //手势开始
                //判断手势落点位置是否在row上
          
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值