美文网首页
多按钮Tab组件

多按钮Tab组件

作者: 小苗晓雪 | 来源:发表于2019-11-12 16:16 被阅读0次
    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 ;
    //初始化某个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; //当前显示页的索引
    
    @end
    
    @implementation TabsView
    
    //初始化
    - (id)initWithFrame:(CGRect)frame WithTitleArr:(NSArray *)titleAr
    {
        self = [super initWithFrame:frame];
        if (self) {
            _currentIndex = -1;
            //title的数量
            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];
                //计算尺寸及位置:
                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];
                //设置文字及字体颜色:
                [btn setTitle:[titleAr objectAtIndex:i] forState:UIControlStateNormal];
                [btn setTitleColor:BTNBASECOLOR forState:UIControlStateNormal];
                //第一个按钮,左侧圆角:
                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;
                }
                //最后一个按钮,右侧圆角:
                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;
                }
               /*
                //如果不是最后一个按钮,需要添加一个分隔竖线
                if (i < (titleCount - 1)) {
                    
                    //计算尺寸及位置
                    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];
                }
               
               */
                //点击事件
                [btn addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchUpInside];
                [_btnArr addObject:btn];
                [self addSubview:btn];
            }
        }
        return self;
    }
    
    
    //按钮点击事件:
    - (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) {
            //当前点击的按钮:
            if (idx == toIndex)
            {
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [btn setBackgroundColor:BTNBASECOLOR];
            }
            //其他按钮
            else
            {
                [btn setTitleColor:BTNBASECOLOR forState:UIControlStateNormal];
                [btn setBackgroundColor:[UIColor whiteColor]];
            }
        }];
        //点击事件:
        self.onClickTitle(sender, self.currentIndex, toIndex);
        self.currentIndex = toIndex;
    }
    
    //初始化某个title
    - (void)initTitle:(NSInteger)toIndex
    {
        [self.btnArr enumerateObjectsUsingBlock:^(UIButton *btn, NSUInteger idx, BOOL * _Nonnull stop) {
            //当前点击的按钮:
            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
    
    

    愿编程让这个世界更美好

    相关文章

      网友评论

          本文标题:多按钮Tab组件

          本文链接:https://www.haomeiwen.com/subject/endqictx.html