UICollectionViewCell的移动和删除(iOS9.0)

最近工程里面需要添加一个UICollectionViewCell的移动和删除的效果,查看了很多网上的资料发现,很多人写的cell移动的效果其实不是移动的效果,只是个交换的效果 。还有删除的效果发现这方面的资料太少了,所以自己写了这篇文章。欢迎批评,吐槽!!!

一.效果图

UICollectionViewCell的移动和删除(iOS9.0)_第1张图片
2016-07-22_13-22-28.gif

二.不啰嗦,上代码

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.array = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", @"4",
              @"5", @"6", @"7", @"8",
              @"9", @"10", @"11", @"12",@"13", @"14", @"15", @"16",
              @"17", @"18", @"19", @"20",  nil];
//长按手势添加到self.collectionView上面
    _longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lonePressMoving:)];
    [self.collectionView addGestureRecognizer:_longPress];
    [self.view addSubview:self.collectionView];
}

懒加载和布局collectionview的代码

//懒加载
- (NSMutableArray *)array{
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}
//UICollectionViewCell布局代码
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.minimumLineSpacing = 10;
        _flowLayout.minimumInteritemSpacing = 10;
        _flowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);//分区内边距
    
        CGFloat totalWidth = kWidth-40;
        CGFloat itemWidth = (totalWidth)/3.0;
        CGFloat itemHeght = 33;
    //注意:item的宽高必须要提前算好
        _flowLayout.itemSize = CGSizeMake(itemWidth, itemHeght);
    //创建collectionView对象,并赋值布局
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, kWidth, kHeight - 20) collectionViewLayout:_flowLayout];
        _collectionView.dataSource = self;

        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        [_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentiifer"];
    }
    return _collectionView;
}

长按手势事件判断->核心代码

- (void)lonePressMoving:    (UILongPressGestureRecognizer *)longPress
{
    switch (_longPress.state) {
        case UIGestureRecognizerStateBegan: {
            {
                NSIndexPath *selectIndexPath = [self.collectionView indexPathForItemAtPoint:[_longPress locationInView:self.collectionView]];
            // 找到当前的cell
                CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:selectIndexPath];
            // 定义cell的时候btn是隐藏的, 在这里设置为NO
                [cell.btnDelete setHidden:NO];
            
                cell.btnDelete.tag = selectIndexPath.item;
            
            //添加删除的点击事件
                [cell.btnDelete addTarget:self action:@selector(btnDelete:) forControlEvents:UIControlEventTouchUpInside];
            
                [_collectionView beginInteractiveMovementForItemAtIndexPath:selectIndexPath];
            }
            break;
        }
        case UIGestureRecognizerStateChanged: {
            [self.collectionView updateInteractiveMovementTargetPosition:[longPress locationInView:_longPress.view]];
            break;
        }
        case UIGestureRecognizerStateEnded: {
            [self.collectionView endInteractiveMovement];
            break;
        }
        default: [self.collectionView cancelInteractiveMovement];
            break;
        }
}

UICollectionViewCell的代理事件

#pragma mark---UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentiifer" forIndexPath:indexPath];
    cell.lable.text = self.array[indexPath.item];
    cell.btnDelete.hidden = YES;
    return cell;
}


#pragma mark---UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@",self.array[indexPath.item]);
}

//添加代码
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath
{
    NSIndexPath *selectIndexPath = [self.collectionView indexPathForItemAtPoint:[_longPress locationInView:self.collectionView]];
// 找到当前的cell
    CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:selectIndexPath];
    cell.btnDelete.hidden = YES;
/*1.存在的问题,移动是二个一个移动的效果*/
//  [collectionView moveItemAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
/*2.存在的问题:只是交换而不是移动的效果*/
//    [self.array exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
 /*3.完整的解决效果*/
    //取出源item数据
    id objc = [self.array objectAtIndex:sourceIndexPath.item];
    //从资源数组中移除该数据
    [self.array removeObject:objc];
    //将数据插入到资源数组中的目标位置上
    [self.array insertObject:objc atIndex:destinationIndexPath.item];
    [self.collectionView reloadData];
}

//删除代码
#pragma mark---btn的删除cell事件

- (void)btnDelete:(UIButton *)btn{
    //cell的隐藏删除设置
    NSIndexPath *selectIndexPath = [self.collectionView indexPathForItemAtPoint:[_longPress locationInView:self.collectionView]];
    // 找到当前的cell
    CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:selectIndexPath];
    cell.btnDelete.hidden = NO;

    //取出源item数据
    id objc = [self.array objectAtIndex:btn.tag];
    //从资源数组中移除该数据
    [self.array removeObject:objc];
    [self.collectionView reloadData];
}

三. 实现功能---注意事项

3.1移动cell的主要注意点

/*1.存在的问题,移动是二个一个移动的效果*/
//    [collectionView moveItemAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
/*2.存在的问题:只是交换而不是移动的效果*/
//    [self.array exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
/*3.完整的解决效果*/
    //取出源item数据
id objc = [self.array objectAtIndex:sourceIndexPath.item];
//从资源数组中移除该数据
[self.array removeObject:objc];
//将数据插入到资源数组中的目标位置上
[self.array insertObject:objc atIndex:destinationIndexPath.item];
[self.collectionView reloadData];

3.2遇到一个问题:长安手势不是很灵敏

发现的问题是:自己将长安的手势添加到了具体的每个cell上面了.
解决的方法:将长按手势添加到self.collectionView上即可.

项目:https://github.com/jinweicheng/UICollectionViewCell-

你可能感兴趣的:(UICollectionViewCell的移动和删除(iOS9.0))