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;

            }

        }

}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章