聯(lián)動(dòng)菜單的實(shí)現(xiàn)方案探索

前言

開(kāi)發(fā)中,經(jīng)常用到分頁(yè)滾動(dòng)菜單的功能點(diǎn),底部頁(yè)面滾動(dòng),頂部的菜單標(biāo)題也會(huì)隨著頁(yè)面的滾動(dòng)位置隨之進(jìn)行切換,這樣的效果實(shí)際上在項(xiàng)目中常常能用上。為了以最快的速度去實(shí)現(xiàn)該功能,這里對(duì)這樣的滾動(dòng)菜單做了一個(gè)封裝,簡(jiǎn)單說(shuō)一下實(shí)現(xiàn)的過(guò)程。

分析

簡(jiǎn)單來(lái)看,由于滾動(dòng)菜單點(diǎn)擊切換并且切換對(duì)應(yīng)的頁(yè)面可以去控制菜單的item有個(gè)聯(lián)動(dòng)的過(guò)程。所以封裝的時(shí)候也主要對(duì)外暴露幾個(gè)重要屬性內(nèi)容:

  • 菜單item點(diǎn)擊回調(diào)
  • 菜單item的設(shè)置屬性
  • 標(biāo)題數(shù)組的設(shè)置

實(shí)現(xiàn)

這樣的滾動(dòng)菜單無(wú)疑是布局以及點(diǎn)擊跳轉(zhuǎn)跟聯(lián)動(dòng)邏輯的結(jié)合。

布局

由于菜單的布局受菜單的item的數(shù)目約束,考慮到屏幕的寬度與菜單展示的總長(zhǎng)度關(guān)系,這里對(duì)菜單的展示做了個(gè)適配,即如果沒(méi)有超出屏幕菜單的item的間隔等距。如果超出屏幕則按照固定的距離添加對(duì)應(yīng)的菜單上。

- (void)setTitleArray:(NSArray *)titleArray{
    
    _titleArray = titleArray;
    
    
    [self setUI];
    
    self.isBlock = YES;
    
    NSInteger btnOffset = 0;
    
    //判斷要添加的item是否超出屏幕,如果沒(méi)有,等分
    BOOL isMore = [self isMoreScreenWidth];

    if (isMore) {
        
        
        for (int i = 0; i < titleArray.count; i++) {
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitle:self.titleArray[i] forState:UIControlStateNormal];
            [btn setTitleColor:LCColorRGB(74, 74, 74) forState:UIControlStateNormal];
            [btn setTitleColor:LCColorRGB(173, 135, 72) forState:UIControlStateSelected];
            btn.titleLabel.font = [UIFont systemFontOfSize:15];
            btn.tag = i;
            
            [btn sizeToFit];
            float originX =  i? 37+btnOffset:18;
            btn.frame = CGRectMake(originX, 0, btn.frame.size.width, 48);
            
            btnOffset = CGRectGetMaxX(btn.frame);
            
            btn.titleLabel.textAlignment = NSTextAlignmentLeft;
            [btn addTarget:self action:@selector(changeSelectedState:) forControlEvents:UIControlEventTouchUpInside];
            
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
            
            [self.scrollView addSubview:btn];
            [self.buttonsArray addObject:btn];
            
            //默認(rèn)選擇第一個(gè)
            if (i == 0) {
                
                btn.selected = YES;
                btn.titleLabel.font = [UIFont systemFontOfSize:15];
                self.currentSelectBtn = btn;
                
                self.bottomBarView.frame = CGRectMake(btn.frame.origin.x, self.scrollView.frame.size.height-2, btn.frame.size.width, 2);
                [self.scrollView addSubview:self.bottomBarView];
                
            }
            
            
        }

        
        
    }else{
        
        //計(jì)算等分之后的間隙大小
        CGFloat interValWidth = (SCREEN_WIDTH - self.totalTitleWidth) / (self.titleArray.count + 1);
        
        
        for (int i = 0; i < titleArray.count; i++) {
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitle:self.titleArray[i] forState:UIControlStateNormal];
            [btn setTitleColor:LCColorRGB(74, 74, 74) forState:UIControlStateNormal];
            [btn setTitleColor:LCColorRGB(173, 135, 72) forState:UIControlStateSelected];
            btn.titleLabel.font = [UIFont systemFontOfSize:15];
            btn.tag = i;
            
            [btn sizeToFit];
            float originX =  i? interValWidth+btnOffset:interValWidth;
            btn.frame = CGRectMake(originX, 0, btn.frame.size.width, 48);
            
            btnOffset = CGRectGetMaxX(btn.frame);
            
            
            btn.titleLabel.textAlignment = NSTextAlignmentLeft;
            [btn addTarget:self action:@selector(changeSelectedState:) forControlEvents:UIControlEventTouchUpInside];
            
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
            
            [self.scrollView addSubview:btn];
            [self.buttonsArray addObject:btn];
            
            //默認(rèn)選擇第一個(gè)
            if (i == 0) {
                
                btn.selected = YES;
                btn.titleLabel.font = [UIFont systemFontOfSize:15];
                self.currentSelectBtn = btn;
                
                self.bottomBarView.frame = CGRectMake(btn.frame.origin.x, self.scrollView.frame.size.height-2, btn.frame.size.width, 2);
                [self.scrollView addSubview:self.bottomBarView];
                
            }
            
            
        }

        
        
    }
    
    
    
    //更新scrollView的內(nèi)容區(qū)域
    self.scrollView.contentSize = CGSizeMake(btnOffset+18, self.scrollView.frame.size.height);
    

}

- (BOOL)isMoreScreenWidth{

    CGFloat totalWidth = 0;
    
    totalWidth += 18;
    
    for (int i = 0; i<self.titleArray.count; i++) {
        
        UILabel *label = [UILabel new];
        label.font = [UIFont systemFontOfSize:15];
        label.text = self.titleArray[i];
        
        [label sizeToFit];
        totalWidth += label.frame.size.width;
        totalWidth += 22;
        
        self.totalTitleWidth += label.frame.size.width;
    }
    
    
    if (totalWidth < SCREEN_WIDTH - 18) {
        
        return NO;
        
    }
    
    return YES;
    
}

點(diǎn)擊item回調(diào)

為了保證點(diǎn)擊菜單的item能夠進(jìn)行外部業(yè)務(wù)邏輯的處理,為此使用了Block進(jìn)行事件的回調(diào)。

- (void)changeSelectedState:(UIButton *)button{
    
    self.currentSelectBtn.selected = NO;
    self.currentSelectBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    
    self.currentSelectBtn = button;
    
    self.currentSelectBtn.selected = YES;
    self.currentSelectBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [UIView animateWithDuration:0.2 animations:^{
        
        if (button.tag == 0) {
            
            self.bottomBarView.frame = CGRectMake(self.currentSelectBtn.frame.origin.x, self.scrollView.frame.size.height - 2, self.currentSelectBtn.frame.size.width, 2);
            [self.scrollView scrollRectToVisible:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height) animated:YES];
            
        }else{
            
            UIButton *preButton = self.buttonsArray[button.tag - 1];
            
            float offsetX = CGRectGetMinX(preButton.frame) - 18;
            
            [self.scrollView scrollRectToVisible:CGRectMake(offsetX, 0, self.scrollView.frame.size.width, button.frame.size.height) animated:YES];
            
            self.bottomBarView.frame = CGRectMake(self.currentSelectBtn.frame.origin.x, self.scrollView.frame.size.height-2, self.currentSelectBtn.frame.size.width, 2);
        }
        
//        self.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH *button.tag, 0);
        
        if(self.pageSelectBlock && self.isBlock){
        
            NSLog(@"current seleted menu is %ld",button.tag);
            self.currentPage = button.tag;  //更新當(dāng)前的curPage
            self.pageSelectBlock(button.tag);
            
        }
        
        //默認(rèn)將傳遞打開(kāi)
        self.isBlock = YES;
        
    }];

}

對(duì)外暴露設(shè)置當(dāng)前菜單item的屬性

為了保證外部能夠控制當(dāng)前的菜單的item的選中位置,所以提供了一個(gè)修改當(dāng)前菜單的index的屬性。

- (void)setCurrentPage:(NSInteger)currentPage{
    
    //防止重復(fù)設(shè)置
    if (_currentPage == currentPage) {
        
        return;
    }
    
    _currentPage = currentPage;

    if (self.titleArray.count == 0) {
        return;
    }
    
    
    self.isBlock = NO;
    //改變當(dāng)前的按鈕狀態(tài)以及偏移對(duì)應(yīng)的菜單
    UIButton *currentBtn = self.buttonsArray[currentPage];
    [self changeSelectedState:currentBtn];
    
}

注意點(diǎn)

這里需要注意的技術(shù)點(diǎn)在于邊緣菜單item點(diǎn)擊問(wèn)題,如果被遮擋部分的item菜單點(diǎn)擊的話,我們需要將當(dāng)前的菜單露出來(lái)以保證能夠被用戶看到具體的菜單內(nèi)容,這里可以通過(guò)scrollView 的scrollRectToVisible方法進(jìn)行調(diào)整并滾動(dòng)到可見(jiàn)位置來(lái)保證菜單的滾動(dòng)效果以及顯示特性。

具體Demo可以看這個(gè):
https://github.com/cclbj/LCHangMenuDemo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,936評(píng)論 25 709
  • 問(wèn)答題47 /72 常見(jiàn)瀏覽器兼容性問(wèn)題與解決方案? 參考答案 (1)瀏覽器兼容問(wèn)題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,118評(píng)論 1 92
  • 我們不辭而別得很默契 在你的成長(zhǎng)過(guò)程中,你有沒(méi)有遇到過(guò)這樣一些人?你和他們的交匯,像是兩顆流星劃出的線,在某個(gè)點(diǎn)相...
    寶寶不笑閱讀 428評(píng)論 0 0
  • 01 迷迷糊糊張開(kāi)了雙眼,便看到頭頂搖搖欲墜,發(fā)出微光的燈泡,才意識(shí)到,我被關(guān)在一共籠子里。‘’嗚....‘’旁邊...
    生途記錄閱讀 245評(píng)論 0 0
  • 看《返老還童》 Benjiemin . Button他有著一般人的內(nèi)心,但是非一般人的經(jīng)歷。本劇是圍繞不同視角的人...
    實(shí)施閱讀 862評(píng)論 0 1

友情鏈接更多精彩內(nèi)容