| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- //
- // 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; i<self.btnArrs.count; i++) {
- UIButton *btn = self.btnArrs[i];
- btnX = (btn.tag-TabBarTypeHome<2?(btn.tag-TabBarTypeHome):(btn.tag-TabBarTypeHome+1))*btnW;
- btn.frame = CGRectMake(btnX, btnY, btnW,btnH);
-
- }
- self.cameraBtn.frame = CGRectMake(2*btnW, btnY, btnW,btnH);
-
- [UIView animateWithDuration:0.25 animations:^{
- self.line.frame = CGRectMake(btnW/4+btnW*(self.selectIndex<2?self.selectIndex:self.selectIndex+1),btnH-4, btnW/2, 2);
- }];
- }
- - (UIButton *)redpointBtn {
- if (!_redpointBtn) {
- _redpointBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _redpointBtn.titleLabel.font = SYS_Font(13);
- [_redpointBtn setTitleColor:UIColor.whiteColor forState:0];
- _redpointBtn.backgroundColor = UIColor.redColor;
- _redpointBtn.userInteractionEnabled = NO;
- _redpointBtn.layer.cornerRadius = 9;
- }
- return _redpointBtn;
- }
- -(void)updateNum:(int)num {
- if (num>0) {
- _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
|