iOS开发 用UICollectionView仿网易新闻的滚动按钮效果

iOS开发时如果用到了类似于网易新闻的头部滚动视图按钮,有兴趣的可以参照下,我用

UICollectionView实现了一个,效果图如下:




下面直接上代码:


第一步,首先在视图控制器中实例化:

 //1.初始化layout

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

    layout1.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    //设置headerView的尺寸大小

    //layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 100);

    //该方法也可以设置itemSize

    layout1.itemSize =CGSizeMake(LXP_SCREEN_WIDTH/4,45);

    // 设置列的最小间距

    layout1.minimumInteritemSpacing = 5;

    // 设置最小行间距

    layout1.minimumLineSpacing = 0;

    //2.初始化collectionView

    _typeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, LXP_SCREEN_WIDTH,45) collectionViewLayout:layout1];

    _typeCollectionView.backgroundColor = [UIColor whiteColor];

    _typeCollectionView.dataSource = self;

    _typeCollectionView.delegate = self;

    _typeCollectionView.showsVerticalScrollIndicator = NO;

    _typeCollectionView.showsHorizontalScrollIndicator = NO;

    [_typeCollectionView registerClass:[DSHChoiceTypeCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];


第二步:实现cell ,cell自己实现只要在cell上加一个Label,宽度随着按钮文字宽度变化即可,然后返回按钮个数即可,最重点的是点击cell的效果,点击一个,其他是灰色,选中的是选中的颜色,下面是重点.


第三步:实例化一个数组,保存选中的cellItem,例如数组名是_rightLabelArray,则现在代理方法中实现下面俩代理:

//UICollectionView被选中时调用的方法

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

                                                                                      *)indexPath {

 

 //点击后添加因为只需要一个所以直接初始化掉重新赋值

        _rightLabelArray = [NSMutableArray arrayWithObject:_nodeArr[indexPath.item]];

        [collectionView reloadData];

  

}


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

{

   

   DSHChoiceTypeCollectionViewCell *cell = (DSHChoiceTypeCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

        DSHMerchantTypeModel *MO = _nodeArr[indexPath.row];

        [cell setNodeTitle:MO.nodeName];

        [cell setBackgroundColor:[UIColor whiteColor]];

        

        // 选中一个的方法

        

        if (_rightLabelArray.count ==0)

        {

            

            // 第一次加载的方法

            if (indexPath.row ==0

                ) {

                cell.redLine.hidden = NO;

                cell.nodeLabel.textColor = [UIColor navigationTintColor];

                cell.nodeLabel.font = BIGTEXTFONTSIZE;

            }else{

                cell.redLine.hidden = YES;

                cell.nodeLabel.textColor = LXP_TEXT_NORMCOLOR;

                cell.nodeLabel.font = SMALLTEXTFONTSIZE;

            }

        }else{

            if ([_rightLabelArray containsObject:_nodeArr[indexPath.item]])

            {

                cell.redLine.hidden = NO;

                cell.nodeLabel.textColor = [UIColor navigationTintColor];

                cell.nodeLabel.font = BIGTEXTFONTSIZE;

            } else {

                cell.redLine.hidden = YES;

                cell.nodeLabel.textColor = LXP_TEXT_NORMCOLOR;

                cell.nodeLabel.font = SMALLTEXTFONTSIZE;

            }

        }

}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值