iOS 無限添加的標題滑塊

來源:互聯網
上載者:User

標籤:control   turn      com   添加   selector   WAD   字型   分享   

先上個

 

 

 

自己寫了無限滑塊的類 繼承UIView 

直接使用 init 方法 可以定義每個標題 的顏色 底線 顏色 選中的顏色 和frame 標題的font

 

直接上代碼:

EJ_sliderHeader.h 

#import <UIKit/UIKit.h>

 

@interface EJ_sliderHeader : UIView

 

-(instancetype)initWithArray:(NSArray*)Data WithSliderColor:(UIColor*)slidercolor WithTitleColor:(UIColor*)titleColor WithtitleFont:(UIFont*)font WithBlock:(void(^)(NSInteger))Block;

 

/**

 無限延伸的滑塊標題

 

 @param frame 整個空間frame

 @param Data string數組

 @param slidercolor 底線和選中標題顏色

 @param titleColor 標題顏色

 @param font 標題字型

 @param Block 回調方法

 @return 執行個體

 */

-(instancetype)initWithFrame:(CGRect)frame WithArray:(NSArray*)Data WithSliderColor:(UIColor*)slidercolor WithTitleColor:(UIColor*)titleColor WithtitleFont:(UIFont*)font WithBlock:(void(^)(NSInteger))Block;

 

 

 

@property(nonatomic,copy) void(^clickToSendBack)(NSInteger index) ;

 

 

/**

 跳轉到指定位置

 

 @param index 下標代表要選中的標題

 */

-(void)changeIndex:(NSInteger)index ;

-(void)indexPathRowAdd ;

-(void)indexPathRowCut ;

@end

.m檔案

EJ_sliderHeader.m

 

//

//  EJ_sliderHeader.m

//  ejlShop

//

//  Created by yunfutai on 2018/2/7.

//  Copyright ? 2018年 JasonGor. All rights reserved.

//

 

#import "EJ_sliderHeader.h"

@interface EJ_sliderHeader()

@property(nonatomic,strong) UIColor * BTNTitleColor ;

@property(nonatomic,strong) UIColor * sliderColor ;

@property(nonatomic,strong) UIFont * btnFont ;

@end

@implementation EJ_sliderHeader{

    UIScrollView * _scrollerBackground;

    NSArray * dataArray;

    UIView * sliderLine ;

    int selecedIndex ;

    

}

#define buttonWidth 83

#define pageScrollerCount 4

-(instancetype)initWithArray:(NSArray *)Data WithSliderColor:(UIColor *)slidercolor WithTitleColor:(UIColor *)titleColor WithtitleFont:(UIFont *)font WithBlock:(void (^)(NSInteger))Block{

    if (self = [super initWithFrame:CGRectMake(0, adaIPhoneX(64), EJScreenW, 45)]) {

       

        dataArray                          = Data;

        _BTNTitleColor                      = titleColor ;

        _sliderColor                        = slidercolor ;

        _btnFont                            = font;

        self.clickToSendBack = Block;

         [self makeupSubView];

    }

    

    return self;

}

 

-(instancetype)initWithFrame:(CGRect)frame WithArray:(NSArray *)Data WithSliderColor:(UIColor *)slidercolor WithTitleColor:(UIColor *)titleColor WithtitleFont:(UIFont *)font WithBlock:(void (^)(NSInteger))Block{

    

    if (self = [super initWithFrame:frame]) {

        

        dataArray                          = Data;

        _BTNTitleColor                      = titleColor ;

        _sliderColor                        = slidercolor ;

        _btnFont                            = font; 

        self.clickToSendBack = Block;

        _scrollerBackground = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];

        _scrollerBackground.pagingEnabled = NO ;

        _scrollerBackground.showsHorizontalScrollIndicator = NO ;

        [self addSubview:_scrollerBackground];

        [self makeupSubView];

        

        self.layer.shadowColor = [UIColor blackColor].CGColor;

        self.layer.shadowOpacity = 0.2f ;

        self.layer.shadowOffset = CGSizeMake(1, 4);

    }

    

    return self;

}

 

 

-(void)makeupSubView{

   CGFloat btnWidth;

    if (dataArray.count>pageScrollerCount) {

        btnWidth =buttonWidth;

        _scrollerBackground.contentSize = CGSizeMake(btnWidth*(dataArray.count), self.height);

        _scrollerBackground.backgroundColor = [UIColor whiteColor];

    }else{

        btnWidth = self.width/dataArray.count ;

    }

    selecedIndex = 0 ;

    for (int i=0; i<dataArray.count; i++) {

        UIButton * btn =[UIButton new];

        [_scrollerBackground addSubview:btn];

        btn.sd_layout

        .topEqualToView(_scrollerBackground)

        .leftSpaceToView(_scrollerBackground, i*btnWidth)

        .widthIs(btnWidth)

        .heightIs(_scrollerBackground.height);

        btn.tag=i ;

        [btn setTitle:dataArray[i] forState:UIControlStateNormal];

        [btn.titleLabel setFont:_btnFont];

        if (i==0) {

          [btn setTitleColor:_sliderColor forState:UIControlStateNormal];

        }else{

          [btn setTitleColor:_BTNTitleColor forState:UIControlStateNormal];

        }

         [btn addTarget:self action:@selector(btnMethod:) forControlEvents:UIControlEventTouchUpInside];

        [btn.titleLabel setFont:EJFont_DIY6(12)];

        btn.backgroundColor = [UIColor whiteColor];

 

        

    }

    for (int i =1 ; i<dataArray.count; i++) {

        UIView * line = [[UIView alloc]initWithFrame:CGRectMake(btnWidth*i, 0, 0.5, self.height)];

        line.backgroundColor = RGBA(241, 241, 241, 1);

        [_scrollerBackground addSubview:line];

    }

    

    UIView * bomLine = [[UIView alloc]initWithFrame:CGRectMake(0, self.height -0.5, EJScreenW , 0.5)];

    bomLine.backgroundColor = RGBA(241, 241, 241, 1);

    [_scrollerBackground addSubview:bomLine];

    

    sliderLine = [[UIView alloc]initWithFrame:CGRectMake(0, self.height - 1, btnWidth, 2)];

    sliderLine.backgroundColor = _sliderColor ;

    [_scrollerBackground addSubview:sliderLine];

    

    

}

#pragma mark -按鈕點擊事件

-(void)btnMethod:(UIButton*)sender{

    for (UIView * sub in _scrollerBackground.subviews) {

        if ([sub isKindOfClass:[UIButton class]] ) {

            UIButton * subBTN = (UIButton*)sub;

            [subBTN setTitleColor:_BTNTitleColor forState:UIControlStateNormal];

            

        }

        

    }

    [sender setTitleColor:_sliderColor forState:UIControlStateNormal];

    CGFloat btnWidth;

    if (dataArray.count>pageScrollerCount) {

        btnWidth = buttonWidth ;

    }else{

        btnWidth = self.width/dataArray.count ;

    }

    selecedIndex = (int)sender ;

    

    [UIView animateWithDuration:0.3f animations:^{

        sliderLine.x = sender.tag *btnWidth;

        

        

    } completion:^(BOOL finished) {

        self.clickToSendBack(sender.tag);

    }];

    

}

#pragma mark -根據下標改變line

-(void)changeIndex:(NSInteger)index{

    for (UIView * sub in self.subviews) {

        if ([sub isKindOfClass:[UIButton class]] ) {

            UIButton * subBTN = (UIButton*)sub;

            [subBTN setTitleColor:_BTNTitleColor forState:UIControlStateNormal];

            

            if (subBTN.tag == index) {

                  [subBTN setTitleColor:_sliderColor forState:UIControlStateNormal];

            }

        }

        

    }

    selecedIndex = (int)index ;

  

    CGFloat btnWidth = self.width/dataArray.count ;

    

    

    [UIView animateWithDuration:0.3f animations:^{

        sliderLine.x = index *btnWidth;

        

        

    } completion:^(BOOL finished) {

        self.clickToSendBack(index);

    }];

}

 

 

-(void)indexPathRowAdd {

    

    selecedIndex ++ ;

    

    if (selecedIndex == dataArray.count) {

        selecedIndex=0 ;

    }

    

    

    for (UIView * sub in self.subviews) {

        if ([sub isKindOfClass:[UIButton class]] ) {

            UIButton * subBTN = (UIButton*)sub;

            [subBTN setTitleColor:_BTNTitleColor forState:UIControlStateNormal];

            

            if (subBTN.tag == selecedIndex) {

                [subBTN setTitleColor:_sliderColor forState:UIControlStateNormal];

            }

        }

        

    }

 

    

    CGFloat btnWidth = self.width/dataArray.count ;

    

    

    [UIView animateWithDuration:0.3f animations:^{

        sliderLine.x = selecedIndex *btnWidth;

        

        

    } completion:^(BOOL finished) {

        self.clickToSendBack(selecedIndex);

    }];

}

-(void)indexPathRowCut {

    selecedIndex -- ;

    

    if (selecedIndex < 0) {

        selecedIndex= (int)(dataArray.count-1) ;

    }

    

    

    for (UIView * sub in self.subviews) {

        if ([sub isKindOfClass:[UIButton class]] ) {

            UIButton * subBTN = (UIButton*)sub;

            [subBTN setTitleColor:_BTNTitleColor forState:UIControlStateNormal];

            

            if (subBTN.tag == selecedIndex) {

                [subBTN setTitleColor:_sliderColor forState:UIControlStateNormal];

            }

        }

        

    }

    

    

    CGFloat btnWidth = self.width/dataArray.count ;

    

    

    [UIView animateWithDuration:0.3f animations:^{

        sliderLine.x = selecedIndex *btnWidth;

        

        

    } completion:^(BOOL finished) {

        self.clickToSendBack(selecedIndex);

    }];

}

@end

 

iOS 無限添加的標題滑塊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.