iOS标签管理

这个是使用UICollectionView 在iOS9之后新加入的一个方法可以很便捷的实现这个功能。
<span style="font-family: Arial, Helvetica, sans-serif;">定义一个全局的cell以及两个数据源数组:</span>
<pre name="code" class="objc">@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
   
    UICollectionViewCell * cell;
}

@property(nonatomic,retain)UICollectionView *collectionView;

@property(nonatomic,strong)NSMutableArray *array1;
@property(nonatomic,strong)NSMutableArray *array2;


@end
 

接下来ViewDidload里面

 _array1 = [NSMutableArray array];
    _array2 = [NSMutableArray array];
    
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 30)];
    lab.text = @"推荐频道";
    lab.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:lab];

    
    for (int i = 0; i< 20; i++) {
        
        UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn1.frame = CGRectMake(0, 0, 50, 50);
        [btn1 setTitle:[NSString stringWithFormat:@"A%d",i] forState:UIControlStateNormal];
        btn1.userInteractionEnabled = NO;
        btn1.backgroundColor = [UIColor redColor];
        [_array1 addObject:btn1];
    }

    for (int i = 15; i <30; i++) {
        UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn2.frame = CGRectMake(0, 0, 50, 50);
        [btn2 setTitle:[NSString stringWithFormat:@"B%d",i] forState:UIControlStateNormal];
        btn2.userInteractionEnabled = NO;
        [_array2 addObject:btn2];
    }
    
    
    
    //这个用来定义水平滚动还是垂直滚动
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 94, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:layout];
    //layout.minimumLineSpacing = 10;
    //layout.minimumInteritemSpacing = 10;
    
    //注册Cell,必须要有
    [_collectionView registerClass:[UICollectionViewCell class]
        forCellWithReuseIdentifier:@"Cell"];

   
    
    _collectionView.backgroundColor = [UIColor grayColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    
    
    //此处给其增加长按手势,用此手势触发cell移动效果
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];
    [_collectionView addGestureRecognizer:longGesture];
    
    
    [self.view addSubview:_collectionView];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 210, self.view.bounds.size.width, 30)];
    label.backgroundColor = [UIColor yellowColor];
    label.text = @"点击添加";
    label.textAlignment = NSTextAlignmentCenter;
    //[self.view addSubview:label];
</pre> 然后就是  <span style="background-color: rgb(240, 240, 240);">CollectionView 的代理方法</span><p></p><p></p><pre name="code" class="objc">//Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}



//Cell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return _array1.count;
    }else
        return _array2.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //这个cell的标识必须使用上面注册的
    static NSString * CellIdentifier = @"Cell";
     cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    if (!cell) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    }
    
    
    cell.backgroundColor = [UIColor blueColor];
   
    
    if (indexPath.section == 0) {
        [cell addSubview:_array1[indexPath.item]];
        return cell;
    }else
        [cell addSubview:_array2[indexPath.item]];
    return cell;
    
    
}
//定义每个UICollectionView 的 margin (边缘距离)
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    //上左下右~
    return UIEdgeInsetsMake(5, 5, 99, 5);
}



//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}


//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

//允许item移动
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}
//是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{
     [cell removeFromSuperview];
    
    if (indexPath.section == 0) {
        
        UIButton *bb = _array1[indexPath.item];
         [_array1 removeObjectAtIndex:indexPath.item];
        [_array2 insertObject:bb atIndex:_array2.count];
       
        [_collectionView reloadData];
        
    }else {
        
        UIButton *cc = _array2[indexPath.item];
        [_array2 removeObjectAtIndex:indexPath.item];
        [_array1 insertObject:cc atIndex:_array1.count];
        [_collectionView reloadData];
        
    }
    
}


接下来就是最重要的,移动跟替换数据

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

//移动结束,替换数据
-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
    
    //两个section
    
    if (sourceIndexPath.section == 0 && destinationIndexPath.section == 1) {
        UIButton *bb = _array1[sourceIndexPath.item];
        [_array1 removeObjectAtIndex:sourceIndexPath.item];
        [_array2 insertObject:bb atIndex:destinationIndexPath.item];
        
    }else if (sourceIndexPath.section == 1 && destinationIndexPath.section == 0) {
        UIButton *cc = _array2[sourceIndexPath.item];
        [_array2 removeObjectAtIndex:sourceIndexPath.item];
        [_array1 insertObject:cc atIndex:destinationIndexPath.item];
    }
    
    //同一个section
    else if (sourceIndexPath.section == 0 && destinationIndexPath.section == 0) {
      
        UIButton *aa = _array1[sourceIndexPath.item];
        [_array1 removeObjectAtIndex:sourceIndexPath.item];
        [_array1 insertObject:aa atIndex:destinationIndexPath.item];
    }
    
    else {
        UIButton *dd = _array2[sourceIndexPath.item];
        [_array2 removeObjectAtIndex:sourceIndexPath.item];
        [_array2 insertObject:dd atIndex:destinationIndexPath.item];
    }
   
}










 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值