RTabBar.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // RTabBar.m
  3. // YBVideo
  4. //
  5. // Created by yunbao01 on 2023/11/9.
  6. // Copyright © 2023 cat. All rights reserved.
  7. //
  8. #import "RTabBar.h"
  9. @interface RTabBar()
  10. /**
  11. 直播按钮
  12. */
  13. @property (nonatomic,strong) UIButton *cameraBtn;
  14. @property (nonatomic,strong) UIButton *locationBtn;
  15. @property(nonatomic,strong)UIButton *redpointBtn;
  16. /**
  17. 最后选择的按钮
  18. */
  19. @property (nonatomic,strong) UIButton *lastSeletcBtn;
  20. /**
  21. 线条
  22. */
  23. @property (nonatomic,strong) UIView *line;
  24. @property (nonatomic,strong) NSMutableArray *btnArrs;
  25. @end
  26. @implementation RTabBar
  27. - (instancetype)initWithFrame:(CGRect)frame
  28. {
  29. self = [super initWithFrame:frame];
  30. if (self) {
  31. [self addSubview:self.tabBarBJView];
  32. [self setupUI];
  33. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeTabarBg:) name:@"changTabBarColor" object:nil];
  34. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(UpdateCity:) name:@"UpdateCity" object:nil];
  35. }
  36. return self;
  37. }
  38. - (void)setupUI{
  39. [self.btnArrs removeAllObjects];
  40. for (int i = 0; i < self.itemArr.count; i++) {
  41. //
  42. UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  43. [itemBtn setImage:[UIImage imageNamed:self.itemArr[i]] forState:UIControlStateNormal];
  44. itemBtn.adjustsImageWhenHighlighted = NO ;
  45. [itemBtn setTitle:YZMsg(self.itemArr[i]) forState:UIControlStateNormal];
  46. itemBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
  47. itemBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
  48. [itemBtn setTitleColor:UIColor.whiteColor forState:UIControlStateSelected];
  49. [itemBtn setTitleColor:UIColor.grayColor forState:UIControlStateNormal];
  50. [itemBtn addTarget:self action:@selector(itemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  51. itemBtn.tag = TabBarTypeHome+i;
  52. if (i==0){
  53. itemBtn.selected = YES;
  54. self.lastSeletcBtn = itemBtn;
  55. // self.tabBarBJView.alpha = 0;
  56. }else if (i == 1){
  57. _locationBtn = itemBtn;
  58. }else if (i==2){
  59. [itemBtn addSubview:self.redpointBtn];
  60. }
  61. [self addSubview: itemBtn];
  62. [self.btnArrs addObject:itemBtn];
  63. }
  64. [self addSubview:self.cameraBtn];
  65. [self addSubview:self.line];
  66. self.line.hidden = YES;
  67. }
  68. - (void)itemBtnClick:(UIButton *)sender{
  69. if ([self.delegate respondsToSelector:@selector(tabbar:withBtn:)]){
  70. [self.delegate tabbar:self withBtn:sender.tag];
  71. }
  72. if (sender.tag != TabBarTypeAdd) {
  73. // self.lastSeletcBtn.selected = NO;
  74. // sender.selected = YES;
  75. // self.lastSeletcBtn = sender;
  76. self.selectIndex = sender.tag -100;
  77. [UIView animateWithDuration:0.2 animations:^{
  78. sender.titleLabel.transform = CGAffineTransformMakeScale(1.2, 1.2);
  79. } completion:^(BOOL finished) {
  80. [UIView animateWithDuration:0.2 animations:^{
  81. sender.titleLabel.transform = CGAffineTransformIdentity;
  82. }];
  83. }];
  84. }
  85. }
  86. - (void)setSelectIndex:(NSInteger)selectIndex{
  87. _selectIndex = selectIndex;
  88. for (UIButton *btn in self.btnArrs){
  89. if(btn.tag == selectIndex+TabBarTypeHome){
  90. btn.selected = YES;
  91. }else{
  92. btn.selected = NO;
  93. }
  94. }
  95. [self setNeedsLayout];
  96. [self reloadBottom];
  97. }
  98. - (void)layoutSubviews{
  99. [super layoutSubviews];
  100. self.tabBarBJView.frame = self.bounds;
  101. CGFloat btnX =0;
  102. CGFloat btnY =0;
  103. CGFloat btnW = self.bounds.size.width/(self.itemArr.count+1);
  104. CGFloat btnH = 49;
  105. for (int i = 0; i<self.btnArrs.count; i++) {
  106. UIButton *btn = self.btnArrs[i];
  107. btnX = (btn.tag-TabBarTypeHome<2?(btn.tag-TabBarTypeHome):(btn.tag-TabBarTypeHome+1))*btnW;
  108. btn.frame = CGRectMake(btnX, btnY, btnW,btnH);
  109. }
  110. self.cameraBtn.frame = CGRectMake(2*btnW, btnY, btnW,btnH);
  111. [UIView animateWithDuration:0.25 animations:^{
  112. self.line.frame = CGRectMake(btnW/4+btnW*(self.selectIndex<2?self.selectIndex:self.selectIndex+1),btnH-4, btnW/2, 2);
  113. }];
  114. }
  115. - (UIButton *)redpointBtn {
  116. if (!_redpointBtn) {
  117. _redpointBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  118. _redpointBtn.titleLabel.font = SYS_Font(13);
  119. [_redpointBtn setTitleColor:UIColor.whiteColor forState:0];
  120. _redpointBtn.backgroundColor = UIColor.redColor;
  121. _redpointBtn.userInteractionEnabled = NO;
  122. _redpointBtn.layer.cornerRadius = 9;
  123. }
  124. return _redpointBtn;
  125. }
  126. -(void)updateNum:(int)num {
  127. if (num>0) {
  128. _redpointBtn.hidden = NO;
  129. [_redpointBtn setTitle:[NSString stringWithFormat:@"%d",num] forState:0];
  130. if (num>9) {
  131. _redpointBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
  132. }else {
  133. _redpointBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
  134. }
  135. [_redpointBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  136. make.height.mas_equalTo(18);
  137. if (num<10) {
  138. make.width.height.mas_equalTo(18);
  139. }
  140. make.right.equalTo(_redpointBtn.superview.mas_right);
  141. make.top.equalTo(_redpointBtn.superview.mas_top);
  142. }];
  143. }else {
  144. _redpointBtn.hidden = YES;
  145. }
  146. }
  147. -(void)reloadBottom{
  148. if([Config getCurrentIsShop] && _selectIndex == 0){
  149. _tabBarBJView.image = [PublicObj getImgWithColor:UIColor.whiteColor];
  150. [_cameraBtn setImage:[UIImage imageNamed:@"upload_icon_black"] forState:UIControlStateNormal];
  151. for (UIButton *btn in self.btnArrs){
  152. [btn setTitleColor:UIColor.grayColor forState:0];
  153. [btn setTitleColor:UIColor.blackColor forState:UIControlStateSelected];
  154. }
  155. }
  156. }
  157. -(void)UpdateCity:(NSNotification *)noti{
  158. NSString *cityStr = noti.object;
  159. [_locationBtn setTitle:cityStr forState:0];
  160. }
  161. -(void)changeTabarBg:(NSNotification *)noti{
  162. [self setNeedsLayout];
  163. NSString *is_change_white = noti.object;
  164. if([is_change_white isEqual:@"1"]){
  165. _tabBarBJView.image = [PublicObj getImgWithColor:UIColor.whiteColor];//[UIImage imageNamed:@"tab_bg"];//
  166. [_cameraBtn setImage:[UIImage imageNamed:@"upload_icon_black"] forState:UIControlStateNormal];
  167. for (UIButton *btn in self.btnArrs){
  168. [btn setTitleColor:UIColor.grayColor forState:0];
  169. [btn setTitleColor:UIColor.blackColor forState:UIControlStateSelected];
  170. }
  171. }else{
  172. _tabBarBJView.image = [PublicObj getImgWithColor:Normal_SubColor];
  173. [_cameraBtn setImage:[UIImage imageNamed:@"upload_icon_white"] forState:UIControlStateNormal];
  174. for (UIButton *btn in self.btnArrs){
  175. [btn setTitleColor:UIColor.grayColor forState:0];
  176. [btn setTitleColor:UIColor.whiteColor forState:UIControlStateSelected];
  177. }
  178. }
  179. }
  180. #pragma mark - lazy
  181. ///添加
  182. - (UIButton *)cameraBtn{
  183. if (!_cameraBtn){
  184. //
  185. _cameraBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  186. [_cameraBtn setImage:[UIImage imageNamed:@"upload_icon"] forState:UIControlStateNormal];
  187. [_cameraBtn addTarget:self action:@selector(itemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  188. _cameraBtn.tag = TabBarTypeAdd;
  189. }
  190. return _cameraBtn;
  191. }
  192. -(NSMutableArray *)itemArr{
  193. if (!_itemArr){
  194. NSArray *arr = @[@"首页",@"同城",@"消息",@"我"];
  195. _itemArr = [NSMutableArray arrayWithArray:arr];
  196. }
  197. return _itemArr;
  198. }
  199. -(UIImageView *)tabBarBJView{
  200. if (!_tabBarBJView){
  201. _tabBarBJView = [[UIImageView alloc]initWithImage:[PublicObj getImgWithColor:Normal_SubColor]];
  202. // [UIImage imageNamed:@"tabbar_bg"];
  203. }
  204. return _tabBarBJView;
  205. }
  206. - (UIView *)line{
  207. if (!_line){
  208. _line = [[UIView alloc]initWithFrame: CGRectMake(_window_width/16 , 49-4, _window_height/8, 2)];
  209. _line.backgroundColor = [UIColor whiteColor];
  210. }
  211. return _line;
  212. }
  213. - (NSMutableArray *)btnArrs{
  214. if(!_btnArrs){
  215. _btnArrs = [NSMutableArray array];
  216. }
  217. return _btnArrs;
  218. }
  219. @end