多按鈕Tab組件

image.png

TabsView.h


#import <UIKit/UIKit.h>

@interface TabsView : UIView

@property (nonatomic,copy)void (^onClickTitle)(UIButton *btn,NSInteger currentIndex,NSInteger toIndex);
//初始化
- (id)initWithFrame:(CGRect)frame WithTitleArr:(NSArray *)titleAr ;
//初始化某個(gè)title
- (void)initTitle:(NSInteger)toIndex ;

@end

TabsView.m


#import "TabsView.h"
#define TAG_START 1000
#define BTNHIGHSPACE 8
#define BTNWIDTHSPACE 12
#define BTNBASECOLOR [UIColor colorWithRed:217.0 / 255 green:98.0 / 255 blue:76.0 / 255 alpha:1.0]

@interface TabsView()

@property (nonatomic,strong)NSMutableArray *btnArr;

@property (nonatomic,assign)NSInteger currentIndex; //當(dāng)前顯示頁的索引

@end

@implementation TabsView

//初始化
- (id)initWithFrame:(CGRect)frame WithTitleArr:(NSArray *)titleAr
{
    self = [super initWithFrame:frame];
    if (self) {
        _currentIndex = -1;
        //title的數(shù)量
        NSInteger titleCount = titleAr.count;
        //基座:
        UIView *btnBaseView = [[UIView alloc]initWithFrame:CGRectMake(BTNWIDTHSPACE, BTNHIGHSPACE, self.frame.size.width - 2 * BTNWIDTHSPACE, self.frame.size.height -2*BTNHIGHSPACE)];
        btnBaseView.backgroundColor = BTNBASECOLOR;
        btnBaseView.layer.cornerRadius = 4;
        btnBaseView.layer.borderColor = BTNBASECOLOR.CGColor;
        btnBaseView.layer.borderWidth = 1;
        [self addSubview:btnBaseView];
        _btnArr = [[NSMutableArray alloc]initWithCapacity:titleCount];
        for (int i=0; i < titleCount; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            //計(jì)算尺寸及位置:
            CGFloat width = (btnBaseView.width-2-1*(titleCount-1)) / titleCount;
            CGFloat height = self.frame.size.height - 2 * BTNHIGHSPACE - 2;
            CGFloat x = BTNWIDTHSPACE + 1 + i * (width + 1);
            CGFloat y = BTNHIGHSPACE + 1;
            btn.frame = CGRectMake(x, y, width, height);
            btn.tag = TAG_START + i;//tag
            btn.titleLabel.font = [UIFont systemFontOfSize:16];
            //設(shè)置文字及字體顏色:
            [btn setTitle:[titleAr objectAtIndex:i] forState:UIControlStateNormal];
            [btn setTitleColor:BTNBASECOLOR forState:UIControlStateNormal];
            //第一個(gè)按鈕,左側(cè)圓角:
            if (i == 0)
            {
                UIBezierPath *maskSalePath = [UIBezierPath bezierPathWithRoundedRect:btn.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(3, 3)];
                CAShapeLayer *maskSaleLayer = [[CAShapeLayer alloc] init];
                maskSaleLayer.frame = btn.bounds;
                maskSaleLayer.path = maskSalePath.CGPath;
                btn.layer.mask = maskSaleLayer;
            }
            //最后一個(gè)按鈕,右側(cè)圓角:
            if (i == (titleCount - 1))
            {
                UIBezierPath *maskTerminalPath = [UIBezierPath bezierPathWithRoundedRect:btn.bounds byRoundingCorners:UIRectCornerBottomRight | UIRectCornerTopRight cornerRadii:CGSizeMake(3, 3)];
                CAShapeLayer *maskTerminalLayer = [[CAShapeLayer alloc] init];
                maskTerminalLayer.frame = btn.bounds;
                maskTerminalLayer.path = maskTerminalPath.CGPath;
                btn.layer.mask = maskTerminalLayer;
            }
           /*
            //如果不是最后一個(gè)按鈕,需要添加一個(gè)分隔豎線
            if (i < (titleCount - 1)) {
                
                //計(jì)算尺寸及位置
                CGFloat x = BTNWIDTHSPACE + (i+1)*(btnBaseView.width/titleCount);
                CGFloat y = BTNHIGHSPACE+1;
                CGFloat width = 1;
                CGFloat height = self.frame.size.height - 2 * BTNHIGHSPACE - 2;
                
                UIView *splitLine = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
                splitLine.backgroundColor = BTNBASECOLOR;
                
                [self addSubview:splitLine];
            }
           
           */
            //點(diǎn)擊事件
            [btn addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchUpInside];
            [_btnArr addObject:btn];
            [self addSubview:btn];
        }
    }
    return self;
}


//按鈕點(diǎn)擊事件:
- (void)didClickBtn:(UIButton *)sender
{
    NSInteger toIndex =sender.tag - TAG_START;
    if (self.currentIndex == toIndex)
    {
        return;
    }
    [self.btnArr enumerateObjectsUsingBlock:^(UIButton *btn, NSUInteger idx, BOOL * _Nonnull stop) {
        //當(dāng)前點(diǎn)擊的按鈕:
        if (idx == toIndex)
        {
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [btn setBackgroundColor:BTNBASECOLOR];
        }
        //其他按鈕
        else
        {
            [btn setTitleColor:BTNBASECOLOR forState:UIControlStateNormal];
            [btn setBackgroundColor:[UIColor whiteColor]];
        }
    }];
    //點(diǎn)擊事件:
    self.onClickTitle(sender, self.currentIndex, toIndex);
    self.currentIndex = toIndex;
}

//初始化某個(gè)title
- (void)initTitle:(NSInteger)toIndex
{
    [self.btnArr enumerateObjectsUsingBlock:^(UIButton *btn, NSUInteger idx, BOOL * _Nonnull stop) {
        //當(dāng)前點(diǎn)擊的按鈕:
        if (idx == toIndex)
        {
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [btn setBackgroundColor:BTNBASECOLOR];
        }
        //其他按鈕:
        else
        {
            [btn setTitleColor:BTNBASECOLOR forState:UIControlStateNormal];
            [btn setBackgroundColor:[UIColor whiteColor]];
        }
        
    }];
    self.currentIndex = toIndex;
}

@end

愿編程讓這個(gè)世界更美好

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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