您的位置:首页 > 产品设计 > UI/UE

iOS9 UICollectionView新推出的Item排序方法

2015-11-14 09:05 537 查看
协议签订

创建UICollectionView

指定代理人

添加手势

手势方法实现

代理方法实现

UICollectionView协议签订 添加属性

[code]@interface ZGLSubscribeCell () <UICollectionViewDataSource ,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *subscribeCollectionView;


创建UICollectionView

[code]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];


长按手势方法实现

[code]- (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

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


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

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


抑制item移动

[code]- (NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveFromItemAtIndexPath:(NSIndexPath *)originalIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath {
    /* 可以指定位置禁止交换 */
    if (proposedIndexPath.item == _optionsArr.count) {
        return originalIndexPath;
    } else {
        return proposedIndexPath;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: