// // RTabBar.m // YBVideo // // Created by yunbao01 on 2023/11/9. // Copyright © 2023 cat. All rights reserved. // #import "RTabBar.h" @interface RTabBar() /** 直播按钮 */ @property (nonatomic,strong) UIButton *cameraBtn; @property (nonatomic,strong) UIButton *locationBtn; @property(nonatomic,strong)UIButton *redpointBtn; /** 最后选择的按钮 */ @property (nonatomic,strong) UIButton *lastSeletcBtn; /** 线条 */ @property (nonatomic,strong) UIView *line; @property (nonatomic,strong) NSMutableArray *btnArrs; @end @implementation RTabBar - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self addSubview:self.tabBarBJView]; [self setupUI]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeTabarBg:) name:@"changTabBarColor" object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(UpdateCity:) name:@"UpdateCity" object:nil]; } return self; } - (void)setupUI{ [self.btnArrs removeAllObjects]; for (int i = 0; i < self.itemArr.count; i++) { // UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [itemBtn setImage:[UIImage imageNamed:self.itemArr[i]] forState:UIControlStateNormal]; itemBtn.adjustsImageWhenHighlighted = NO ; [itemBtn setTitle:YZMsg(self.itemArr[i]) forState:UIControlStateNormal]; itemBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18]; itemBtn.titleLabel.adjustsFontSizeToFitWidth = YES; [itemBtn setTitleColor:UIColor.whiteColor forState:UIControlStateSelected]; [itemBtn setTitleColor:UIColor.grayColor forState:UIControlStateNormal]; [itemBtn addTarget:self action:@selector(itemBtnClick:) forControlEvents:UIControlEventTouchUpInside]; itemBtn.tag = TabBarTypeHome+i; if (i==0){ itemBtn.selected = YES; self.lastSeletcBtn = itemBtn; // self.tabBarBJView.alpha = 0; }else if (i == 1){ _locationBtn = itemBtn; }else if (i==2){ [itemBtn addSubview:self.redpointBtn]; } [self addSubview: itemBtn]; [self.btnArrs addObject:itemBtn]; } [self addSubview:self.cameraBtn]; [self addSubview:self.line]; self.line.hidden = YES; } - (void)itemBtnClick:(UIButton *)sender{ if ([self.delegate respondsToSelector:@selector(tabbar:withBtn:)]){ [self.delegate tabbar:self withBtn:sender.tag]; } if (sender.tag != TabBarTypeAdd) { // self.lastSeletcBtn.selected = NO; // sender.selected = YES; // self.lastSeletcBtn = sender; self.selectIndex = sender.tag -100; [UIView animateWithDuration:0.2 animations:^{ sender.titleLabel.transform = CGAffineTransformMakeScale(1.2, 1.2); } completion:^(BOOL finished) { [UIView animateWithDuration:0.2 animations:^{ sender.titleLabel.transform = CGAffineTransformIdentity; }]; }]; } } - (void)setSelectIndex:(NSInteger)selectIndex{ _selectIndex = selectIndex; for (UIButton *btn in self.btnArrs){ if(btn.tag == selectIndex+TabBarTypeHome){ btn.selected = YES; }else{ btn.selected = NO; } } [self setNeedsLayout]; [self reloadBottom]; } - (void)layoutSubviews{ [super layoutSubviews]; self.tabBarBJView.frame = self.bounds; CGFloat btnX =0; CGFloat btnY =0; CGFloat btnW = self.bounds.size.width/(self.itemArr.count+1); CGFloat btnH = 49; for (int i = 0; i0) { _redpointBtn.hidden = NO; [_redpointBtn setTitle:[NSString stringWithFormat:@"%d",num] forState:0]; if (num>9) { _redpointBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5); }else { _redpointBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); } [_redpointBtn mas_remakeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(18); if (num<10) { make.width.height.mas_equalTo(18); } make.right.equalTo(_redpointBtn.superview.mas_right); make.top.equalTo(_redpointBtn.superview.mas_top); }]; }else { _redpointBtn.hidden = YES; } } -(void)reloadBottom{ if([Config getCurrentIsShop] && _selectIndex == 0){ _tabBarBJView.image = [PublicObj getImgWithColor:UIColor.whiteColor]; [_cameraBtn setImage:[UIImage imageNamed:@"upload_icon_black"] forState:UIControlStateNormal]; for (UIButton *btn in self.btnArrs){ [btn setTitleColor:UIColor.grayColor forState:0]; [btn setTitleColor:UIColor.blackColor forState:UIControlStateSelected]; } } } -(void)UpdateCity:(NSNotification *)noti{ NSString *cityStr = noti.object; [_locationBtn setTitle:cityStr forState:0]; } -(void)changeTabarBg:(NSNotification *)noti{ [self setNeedsLayout]; NSString *is_change_white = noti.object; if([is_change_white isEqual:@"1"]){ _tabBarBJView.image = [PublicObj getImgWithColor:UIColor.whiteColor];//[UIImage imageNamed:@"tab_bg"];// [_cameraBtn setImage:[UIImage imageNamed:@"upload_icon_black"] forState:UIControlStateNormal]; for (UIButton *btn in self.btnArrs){ [btn setTitleColor:UIColor.grayColor forState:0]; [btn setTitleColor:UIColor.blackColor forState:UIControlStateSelected]; } }else{ _tabBarBJView.image = [PublicObj getImgWithColor:Normal_SubColor]; [_cameraBtn setImage:[UIImage imageNamed:@"upload_icon_white"] forState:UIControlStateNormal]; for (UIButton *btn in self.btnArrs){ [btn setTitleColor:UIColor.grayColor forState:0]; [btn setTitleColor:UIColor.whiteColor forState:UIControlStateSelected]; } } } #pragma mark - lazy ///添加 - (UIButton *)cameraBtn{ if (!_cameraBtn){ // _cameraBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_cameraBtn setImage:[UIImage imageNamed:@"upload_icon"] forState:UIControlStateNormal]; [_cameraBtn addTarget:self action:@selector(itemBtnClick:) forControlEvents:UIControlEventTouchUpInside]; _cameraBtn.tag = TabBarTypeAdd; } return _cameraBtn; } -(NSMutableArray *)itemArr{ if (!_itemArr){ NSArray *arr = @[@"首页",@"同城",@"消息",@"我"]; _itemArr = [NSMutableArray arrayWithArray:arr]; } return _itemArr; } -(UIImageView *)tabBarBJView{ if (!_tabBarBJView){ _tabBarBJView = [[UIImageView alloc]initWithImage:[PublicObj getImgWithColor:Normal_SubColor]]; // [UIImage imageNamed:@"tabbar_bg"]; } return _tabBarBJView; } - (UIView *)line{ if (!_line){ _line = [[UIView alloc]initWithFrame: CGRectMake(_window_width/16 , 49-4, _window_height/8, 2)]; _line.backgroundColor = [UIColor whiteColor]; } return _line; } - (NSMutableArray *)btnArrs{ if(!_btnArrs){ _btnArrs = [NSMutableArray array]; } return _btnArrs; } @end