ios 类似网易新闻分类中的拖动重排

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    /**

     [flowLayout setHeaderReferenceSize:CGSizeMake(self.view.frame.size.width, 100)];

     

     */

   

    

    self.view.backgroundColor = [UIColor whiteColor];

    self.dataArr = @[@"首页",@"排行",@"国内",@"国际",@"社会",@"评论",@"数读",@"军事",@"航空"].mutableCopy;

    self.secArr = @[@"无人机",@"新闻学院",@"政务",@"公益",@"媒体",@"旅游",@"学习",@"美女"].mutableCopy;

    [self.view addSubview:self.collectionview];

   

    


}



/**

 长按手势

 

 

 */

-(void)longpAction:(UILongPressGestureRecognizer *)sender

{

    CGPoint point = [sender locationInView:sender.view];

    //获取点击的索引

    NSIndexPath *indexPath = [self.collectionview indexPathForItemAtPoint:point];

    

    // 只允许第一区可移动

    if (indexPath.section != 0) {

        return;

    }

    

    switch (sender.state) {

            case UIGestureRecognizerStateBegan: {

                if (indexPath) {

                    [self.collectionview beginInteractiveMovementForItemAtIndexPath:indexPath];

                }

                break;

            }

            case UIGestureRecognizerStateChanged: {//手势状态改变的时候

                [self.collectionview updateInteractiveMovementTargetPosition:point];//刷新

                break;

            }

            case UIGestureRecognizerStateEnded: {

                [self.collectionview endInteractiveMovement];

                break;

            }

        default: {

            [self.collectionview cancelInteractiveMovement];

            break;

        }

    }

    

}

-(void)TapAction:(UITapGestureRecognizer *)sender

{

    sender.view.tag = !sender.view.tag;

    sender.view.tag = sender.view.tag ?(self.isexp = YES):(self.isexp =NO);

    [self.collectionview reloadData];

   

}

#pragma mark - dat/del

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return 2;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    if (section == 0) {

        return self.dataArr.count;

    }else{

        if (self.isexp) {

            return self.secArr.count;

        }else{

            return 0;

        }

    

    }

}

//设置格子大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

    return CGSizeMake(80, 35);


}


/**

 分区的头尾视图标题的设置


 @

 @

 @param indexPath      index


 @return 标题

 */

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

{

    UICollectionReusableView *Rview = nil;

    if ([kind isEqualToString:UICollectionElementKindSectionHeader])

    {

        NSString * headTitles ;

        if (indexPath.section == 0) {

            headTitles = @"切换栏目";

        }else{

            headTitles = @"点击添加更多栏目";

        }

        UICollectionReusableView *view  = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

        UILabel *label = [[UILabel alloc]init];

        label.frame = CGRectMake(15, 0, self.view.frame.size.width-30, 45);

        label.backgroundColor = [UIColor whiteColor];

        label.font = [UIFont systemFontOfSize:15.0];

        label.text = headTitles;

        [view addSubview:label];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapAction:)];

        [view addGestureRecognizer:tap];

        Rview = view;

    }

    return Rview;

    

}

//设置格子上左下右间距

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

    return UIEdgeInsetsMake(5, 10, 0, 10);

}


// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];

    cell.titlesLab.backgroundColor = [UIColor redColor];

    cell.titlesLab.textColor = [UIColor whiteColor];

    if ( indexPath.section == 0) {

        cell.titlesLab.text = self.dataArr[indexPath.row];

        cell.delimgv.image = [UIImage imageNamed:@"58PIC9U58PICwyA_1024"];

    }else{

        cell.titlesLab.text = self.secArr[indexPath.row];

    }

    

    

    return cell;

    

}


/**

 是否可以移动


 @param collectionView self

 @param indexPath index

 

 @return yes

 */

-(BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath

{

    if (indexPath.section == 0) {//只是在第一个中可以移动.

        return YES;

    }

    return NO;

}


/**

 拖动重排


 @param collectionView       self.collectionview

 @param sourceIndexPath      当前点击的数据源位置

 @param destinationIndexPath 移动到的目标位置

 */

-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

    if (sourceIndexPath.section == 0 && destinationIndexPath.section == 0) {

        [self.dataArr exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];

    }

    

}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

   

    if (indexPath.section == 0) {//点击的是第一个

        //相应的第一个减少.第二个增加

        NSString * titles = self.dataArr[indexPath.item];

                     [self.secArr addObject:titles];

                     [self.dataArr removeObjectAtIndex:indexPath.item];

        

    }else{

        NSString * titles = self.secArr[indexPath.item];

        [self.dataArr addObject:titles];

        [self.secArr removeObjectAtIndex:indexPath.item];

    }

    

   

//    NSIndexSet * indexSet = [[NSIndexSet alloc]initWithIndex:indexPath.section];

//   

//    [self.collectionview reloadSections:indexSet];

    [self.collectionview reloadData];

    

//    

    


}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


-(UICollectionView *)collectionview

{

    if (!_collectionview) {

        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];

         [flowLayout setHeaderReferenceSize:CGSizeMake(self.view.frame.size.width, 55)];

        _collectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 15, self.view.frame.size.width, self.view.frame.size.height-60) collectionViewLayout:flowLayout];

        [_collectionview registerClass:[CustomCell class] forCellWithReuseIdentifier:identify];

        //注册头视图

         [_collectionview registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];

        _collectionview.backgroundColor = [UIColor clearColor];

        _collectionview.dataSource = self;

        _collectionview.delegate = self;

        UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc ]initWithTarget:self action:@selector(longpAction:)];

        [_collectionview addGestureRecognizer:longP];

        

    }

    return _collectionview;

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值