iOS分類導(dǎo)航條(二)下劃線的移動(dòng)效果

這個(gè)是對(duì)上篇文章的優(yōu)化丧叽。
上篇下劃線的效果直接是消失和出現(xiàn)骚灸,這篇文章里面改成下劃線的移動(dòng)效果翁巍,不會(huì)顯得突兀驴一。

最重要的注意點(diǎn):
(1)不要把下劃線添加到button里面,要不然怎么著都是出現(xiàn)消失的效果灶壶。(千萬記赘味稀)
(2)可以通過調(diào)節(jié)動(dòng)畫的時(shí)間看效果。

初始化的代碼沒有用到,沒有刪除胸懈,不影響担扑。

只改動(dòng)了XXTitleView.m的代碼,直接上代碼了:

//
//  XXHomeView.m
//  XXTableBar
//
//  Created by shine on 2016/12/26.
//  Copyright ? 2016年 shine. All rights reserved.
//

#import "XXTitleView.h"
//#import "UIView+Extension.h"

@interface XXTitleView ()

//保存所有的button
@property (strong, nonatomic) NSMutableArray *titleButtons;

//titleView
@property (weak, nonatomic) UIScrollView *titleView;

//記錄上一個(gè)按鈕的寬度
@property(nonatomic, assign)CGFloat lastbtnW;

//記錄當(dāng)前選中的按鈕
@property (weak, nonatomic) UIButton *currentButton;

//記錄當(dāng)前選中按鈕的下劃線
@property (strong, nonatomic) UIView *currentLine;

@end

@implementation XXTitleView
#pragma mark -- 懶加載
- (NSMutableArray *)titleButtons{

    if(_titleButtons == nil){
        _titleButtons = [NSMutableArray array];
    }
    return _titleButtons;
}

- (UIView *)currentLine{
    if (_currentLine == nil) {
        _currentLine = [[UIView alloc] init];
        _currentLine.backgroundColor = [UIColor redColor];
    }
    return _currentLine;
}

#pragma mark -- set方法
- (void)setTitleArray:(NSArray *)titleArray{
    if(_titleArray == nil){
        _titleArray = titleArray;
    }
    
    [self setupTitleView];
}

#pragma mark -- 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        //初始化界面
    }
    return self;
}

- (void)setupTitleView{
    //NSLog(@"我是setupTitleView");
    //1.創(chuàng)建titileView
    UIScrollView *titleView = [[UIScrollView alloc] init];
    titleView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    titleView.showsVerticalScrollIndicator = NO;
    titleView.showsHorizontalScrollIndicator = NO;
    titleView.bounces = NO;
    titleView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:titleView];
    self.titleView = titleView;
    
    //2.創(chuàng)建8-10個(gè)按鈕趣钱,并將其加入到titleView
    float btnMargin = 5;
    float btnX = 0;
    for (int i = 0; i < self.titleArray.count; i++) {
        
        //2.1 根據(jù)文字計(jì)算button的寬度
        NSDictionary *arributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14]};
        NSStringDrawingOptions option = NSStringDrawingUsesFontLeading | NSStringDrawingUsesDeviceMetrics | NSStringDrawingUsesLineFragmentOrigin;
        CGSize titleSize = [self.titleArray[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:option attributes:arributes context:nil].size;
        CGFloat textW = titleSize.width + 2 * btnMargin;
        self.lastbtnW = textW;
        btnX = btnX + textW;
        //NSLog(@"btnX = %f",btnX);
        
        //2.2 創(chuàng)建button
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        //button.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
        button.frame = CGRectMake(btnX - self.lastbtnW, 0, textW, self.frame.size.height);
        
        //2.3 設(shè)置button的文字,不要使用button.titleLabel.text,不要使用sizetofit
        [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        
        //2.4 添加button到視圖中
        [self.titleButtons addObject:button];
        [self.titleView addSubview:button];
        
        //2.5添加button點(diǎn)擊事件
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        
        //3.設(shè)置titleView的contentSize
        self.titleView.contentSize = CGSizeMake(CGRectGetMaxX(button.frame), 0);
        
        //4.程序運(yùn)行時(shí)默認(rèn)在第一個(gè)按鈕
        if(i == 0){
            UIButton *firstBtn = self.titleButtons[i];
            self.currentButton = firstBtn;
            self.currentButton.selected = YES;
            //創(chuàng)建一根下劃線涌献,添加到titleView中(千萬不能放在button里面,要不然沒有移動(dòng)的效果首有,而是突然消失和出現(xiàn)的效果)
            //線寬和選中的button一樣寬燕垃,線高為3
            self.currentLine.frame = CGRectMake(0, self.currentButton.frame.size.height - 3, self.currentButton.frame.size.width, 3);
            [self.titleView addSubview:self.currentLine];
        }
    }
}


- (void)clickButton:(UIButton *)button{
    NSLog(@"%d",button.tag);

    //將之前選中的按鈕的selected設(shè)置為no
    //獲取之前選中按鈕的寬度,為計(jì)算伸縮比例作準(zhǔn)備
    CGFloat lastButtonW = self.currentButton.frame.size.width;
    self.currentButton.selected = NO;
    
    //將現(xiàn)在選中的按鈕的selected設(shè)置為yes井联,并將此按鈕賦值給當(dāng)前按鈕卜壕。
    button.selected = YES;
    self.currentButton = button;

    [UIView animateWithDuration:0.1 animations:^{

        CGFloat lineW = self.currentButton.frame.size.width;
        //CGFloat lineH = 3;
                //獲取當(dāng)前按鈕的center并將其賦給下劃線(有問題,從上往下掉為什么)
        CGPoint buttonCenter = self.currentButton.center;
        CGPoint lineCenter = self.currentLine.center;
        lineCenter.x = buttonCenter.x;
        self.currentLine.center = lineCenter;
        //計(jì)算伸縮的比例
        CGFloat sx = lineW / lastButtonW;
        self.currentLine.transform = CGAffineTransformScale(self.currentLine.transform, sx, 1);
    }];
    
    //獲取當(dāng)前按鈕的x值,只有當(dāng)?shù)谌N情況下才需要改變scrollview的偏移
    //1.獲取到當(dāng)前點(diǎn)擊按鈕的x值,加上此按鈕的寬度與中心作比較低矮,如果小于寬度的中心印叁,將titleVIew的偏移量設(shè)置為0
    //2.獲取到當(dāng)前點(diǎn)擊按鈕的x值,減去此按鈕的寬度與中心作比較军掂,如果大于寬度的中心轮蜕,將titleVIew的x偏移量設(shè)置為contenSize-屏幕的寬度
    //3.其他情況。按鈕相對(duì)于屏幕的位移+此按鈕的一半寬度與屏幕中心作對(duì)比蝗锥。
    //獲取當(dāng)前按鈕的x值
    CGFloat buttonX = CGRectGetMaxX(self.currentButton.frame) - self.currentButton.frame.size.width;
    //獲取當(dāng)前scrollView的x偏移量
    CGFloat offsetX = self.titleView.contentOffset.x;
    
    //取得當(dāng)前按鈕此時(shí)相對(duì)于屏幕的值
    CGFloat widthToScreen = buttonX - offsetX;
    
    //獲取屏幕寬度的一半
    CGFloat halfWidth = [[UIScreen mainScreen] bounds].size.width * 0.5;
    
    //當(dāng)前按鈕的x值加上按鈕的一半寬度
    CGFloat buttonXaddHalfWidth = widthToScreen + 0.5 * self.currentButton.frame.size.width;
    
    if ((buttonX + 0.5 * self.currentButton.frame.size.width) < halfWidth) {
        //button的x值+button一半的寬度小于屏幕寬度的一半
        //將titleVIew的偏移量設(shè)置為0
        [self.titleView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    else if ((self.titleView.contentSize.width - buttonX - 0.5 * self.currentButton.frame.size.width < halfWidth)){
        //button的x值-button一半的寬度大于屏幕寬度的一半
        //將titleVIew的x偏移量設(shè)置為contenSize-屏幕的寬度
        CGFloat titleViewOffsetX = self.titleView.contentSize.width - 2 * halfWidth;
        [self.titleView setContentOffset:CGPointMake(titleViewOffsetX, 0) animated:YES];
        
    }
    else{
        //相對(duì)于屏幕的寬度+按鈕寬度的一半<屏幕的中心跃洛, 則向右移一點(diǎn)(即titleVIew的x偏移量減小)
        //相對(duì)于屏幕的寬度+按鈕寬度的一半>屏幕的中心终议, 則向左移一點(diǎn)(即titleVIew的x偏移量增大)
        //改變偏移量
        CGFloat titleViewOffsetX = self.titleView.contentOffset.x - (halfWidth - buttonXaddHalfWidth);
        [self.titleView setContentOffset:CGPointMake(titleViewOffsetX, 0) animated:YES];
    }

}
@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末汇竭,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子穴张,更是在濱河造成了極大的恐慌细燎,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,252評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件皂甘,死亡現(xiàn)場(chǎng)離奇詭異玻驻,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)偿枕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門璧瞬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人渐夸,你說我怎么就攤上這事嗤锉。” “怎么了墓塌?”我有些...
    開封第一講書人閱讀 168,814評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵瘟忱,是天一觀的道長(zhǎng)奥额。 經(jīng)常有香客問我,道長(zhǎng)酷誓,這世上最難降的妖魔是什么披坏? 我笑而不...
    開封第一講書人閱讀 59,869評(píng)論 1 299
  • 正文 為了忘掉前任态坦,我火速辦了婚禮盐数,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘伞梯。我一直安慰自己玫氢,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,888評(píng)論 6 398
  • 文/花漫 我一把揭開白布谜诫。 她就那樣靜靜地躺著漾峡,像睡著了一般。 火紅的嫁衣襯著肌膚如雪喻旷。 梳的紋絲不亂的頭發(fā)上生逸,一...
    開封第一講書人閱讀 52,475評(píng)論 1 312
  • 那天,我揣著相機(jī)與錄音且预,去河邊找鬼槽袄。 笑死,一個(gè)胖子當(dāng)著我的面吹牛锋谐,可吹牛的內(nèi)容都是我干的遍尺。 我是一名探鬼主播,決...
    沈念sama閱讀 41,010評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼涮拗,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼乾戏!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起三热,我...
    開封第一講書人閱讀 39,924評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤鼓择,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后就漾,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體呐能,經(jīng)...
    沈念sama閱讀 46,469評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,552評(píng)論 3 342
  • 正文 我和宋清朗相戀三年从藤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了催跪。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,680評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡夷野,死狀恐怖懊蒸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情悯搔,我是刑警寧澤骑丸,帶...
    沈念sama閱讀 36,362評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響通危,放射性物質(zhì)發(fā)生泄漏铸豁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,037評(píng)論 3 335
  • 文/蒙蒙 一菊碟、第九天 我趴在偏房一處隱蔽的房頂上張望节芥。 院中可真熱鬧,春花似錦逆害、人聲如沸头镊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)相艇。三九已至,卻和暖如春纯陨,著一層夾襖步出監(jiān)牢的瞬間坛芽,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工翼抠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留咙轩,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,099評(píng)論 3 378
  • 正文 我出身青樓机久,卻偏偏與公主長(zhǎng)得像臭墨,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子膘盖,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,691評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容