TabView

作者: 和女神经常玩 | 来源:发表于2022-12-29 23:05 被阅读0次

    接口部分

    @protocol BaseTabViewDelegate <NSObject>
    
    @optional
    
    -(void)baseTabViewSelectedWithSelectIndex:(long)selectIndex;
    
    
    @end
    
    
    @interfaceSBaseTabView : UIView
    
    @property (nonatomic, weak) id<BaseTabViewDelegate> delegate;
    
    @property (nonatomic,assign) long selectIndex;
    
    @property (nonatomic, retain) NSArray<NSString *> * __nullable texts;
    
    @property (nonatomic,strong,nonnull) UIColor *textColor;
    
    -(instancetype)initWithTexts:(NSArray<NSString *> *)texts;
    
    
    @end
    
    

    实现部分

    @interface BaseTabView ()
    
    @property (nonatomic, strong) UIView *contentView;
    
    @property (nonatomic,strong) NSMutableArray<UIButton *> *buttons;
    
    @end
    
    @implementation BaseTabView
    
    
    - (void)setFrame:(CGRect)frame
    {
        [super setFrame:frame];
        [self refreshUI];
    }
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.textColor = [UIColor corlorWithR:51 G:51 B:51];
            self.selectIndex = 0;
        }
        return self;
    }
    -(instancetype)initWithTexts:(NSArray<NSString *> *)texts
    {
        self = [super init];
        if (self) {
            self.texts = texts;
        }
        return self;
    }
    
    -(void)setSelectIndex:(NSInteger)selectIndex
    {
        if (selectIndex >= 0 && selectIndex <= self.texts.count) {
            _selectIndex = selectIndex;
            [self selectButtonWithIndex:selectIndex];
        }
    }
    
    -(void)setTexts:(NSArray<NSString *> *)texts
    {
        _texts = texts;
        [self refreshButtons];
        [self refreshUI];
        self.selectIndex = 0;
    }
    -(void)setTextColor:(UIColor *)textColor
    {
        if (textColor) {
            _textColor = textColor;
            for (UIButton *button in self.buttons) {
                [button setTitleColor:textColor forState:UIControlStateNormal];
            }
        }
    }
    #pragma mark - 内部方法
    
    -(void)refreshButtons
    {
        self.buttons = [NSMutableArray array];
        if (self.contentView) {
            [self.contentView removeFromSuperview];
            self.contentView = nil;
        }
        
        if (self.texts.count == 0) {
            return;
        }
        self.contentView = [[UIView alloc] init];
        [self addSubview:self.contentView];
        
        for (int i = 0; i < self.texts.count; i++) {
            UIButton *button = [self createButtonWithText:self.texts[i]];
            button.tag = i;
            [self.buttons button];
            [self.contentView button];
        }
        
        [self refreshUI];
    }
    
    - (void)refreshUI
    {
        if (self.contentView) {
            self.contentView.frame = self.bounds;
        }
        
        CGFloat buttonW = self.contentView.frame.size.width / self.buttons.count;
        for (int i = 0; i < self.buttons.count; i++) {
            self.buttons[i].frame = CGRectMake(i * buttonW, 0, buttonW, self.contentView.frame.size.height);
        }
    }
    
    -(UIButton *)createButtonWithText:(NSString *)text
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:text forState:UIControlStateNormal];
        [button setTitleColor:self.textColor forState:UIControlStateNormal];
        [button setFont:[UIFont fontWithName:@"PingFangSC-Medium" size:16] isSelected:YES];
        [button setFont:[UIFont fontWithName:@"PingFangSC-Regular" size:13] isSelected:NO];
            
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        return button;
    }
    
    -(void)buttonAction:(UIButton *)button
    {
        if (button.tag != self.selectIndex) {
            self.selectIndex = button.tag;
            if (self.delegate && [self.delegate respondsToSelector:@selector(baseTabViewSelectedWithSelectIndex:)]) {
                [self.delegate baseTabViewSelectedWithSelectIndex:button.tag];
            }
        }
    }
    
    -(void)selectButtonWithIndex:(long)index
    {
        for (int i = 0; i < self.underLineButtons.count; i++) {
            if (index == i) {
                self.buttons[i].selected = YES;
            }
            else
            {
                self.buttons[i].selected = NO;
            }
        }
    }
    
    @end
    
    

    相关文章

      网友评论

          本文标题:TabView

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