iOS9 UICollectionView新推出的Item排序方法

协议签订
创建UICollectionView
指定代理人
添加手势
手势方法实现
代理方法实现

UICollectionView协议签订 添加属性

@interface ZGLSubscribeCell () <UICollectionViewDataSource ,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *subscribeCollectionView;

创建UICollectionView

UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
/* ScaleX pch文件中定义的宏 用于适配屏幕尺寸 */
    flow.itemSize = CGSizeMake(ScaleX * 375 / 3, ScaleX * 375 / 3);
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 0;
    flow.scrollDirection = 0;

    self.subscribeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0,ScaleX * 375, ScaleX * 375) collectionViewLayout:flow];
    /* 指定代理人 */
    self.subscribeCollectionView.delegate = self;
    self.subscribeCollectionView.dataSource = self;
    self.subscribeCollectionView.backgroundColor = [UIColor whiteColor];
    /* UICollectionView注册cell的方法 */
    [self.subscribeCollectionView registerClass:[ZGLSubscribeCollectionViewViewCell class] forCellWithReuseIdentifier:@"ZGLSubscribeCell_ZGLSubscribeCollectionViewViewCell"];
    [self.subscribeCollectionView registerClass:[ZGLAddOptionsCell class] forCellWithReuseIdentifier:@"ZGLSubscribeCell_ZGLAddOptionsCell"];
    /* 此处给其增加长按手势,用此手势触发cell移动效果 */
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];
    [self.subscribeCollectionView addGestureRecognizer:longGesture];
    [self.contentView addSubview:self.subscribeCollectionView];

长按手势方法实现

- (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {
    /* 判断手势状态 */
    switch (longGesture.state) {
        case UIGestureRecognizerStateBegan:{
            /* 判断手势落点位置是否在路径上 */            
            NSIndexPath *indexPath = [self.subscribeCollectionView indexPathForItemAtPoint:[longGesture locationInView:self.subscribeCollectionView]];
            if (indexPath == nil) {
                break;
            }
            /* 在路径上则开始移动该路径上的cell */
            [self.subscribeCollectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
        }
            break;
        case UIGestureRecognizerStateChanged:
            /* 移动过程当中随时更新cell位置 */
            [self.subscribeCollectionView updateInteractiveMovementTargetPosition:[longGesture locationInView:self.subscribeCollectionView]];
            break;
        case UIGestureRecognizerStateEnded:
            /* 移动结束后关闭cell移动 */
            [self.subscribeCollectionView endInteractiveMovement];
            break;
        default:
            [self.subscribeCollectionView cancelInteractiveMovement];
            break;
    }
}

允许移动的item

- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
    /* 返回YES允许其item移动 */
        return YES;
}

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

- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
    /* 交换资源数组中移动item和目标位置item的资源位置 */
        [self.optionsArr exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
        [self.subscribeCollectionView reloadData];
}

抑制item移动

- (NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveFromItemAtIndexPath:(NSIndexPath *)originalIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath {
    /* 可以指定位置禁止交换 */
    if (proposedIndexPath.item == _optionsArr.count) {
        return originalIndexPath;
    } else {
        return proposedIndexPath;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值