SPSBTabBarView.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // SPSBTabBarView.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/4/19.
  6. //
  7. #import "SPSBTabBarView.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #import <CALayer+JXHShadow.h>
  10. #define spsb_itemTag 3000
  11. #define spsb_itemIconTag 4000
  12. #define spsb_itemTipsViewTag 6000
  13. @interface SPSBTabBarView () {
  14. SPSBTabBarType _currentTab;
  15. }
  16. @end
  17. @implementation SPSBTabBarView
  18. - (instancetype)init {
  19. self = [super init];
  20. if (!self) return nil;
  21. self.backgroundColor = spsb_FFFFFF_color(1.F);
  22. [self setTabBarItems];
  23. return self;
  24. }
  25. - (void)chooseTabBar:(SPSBTabBarType)type {
  26. [self clickAction:[self viewWithTag:type + spsb_itemTag]];
  27. }
  28. - (void)tipsViewHidden:(bool)hidden tips:(nullable NSString *)tips tabBarType:(SPSBTabBarType)type {
  29. UIView *view = [[self viewWithTag:type + spsb_itemTag] viewWithTag:spsb_itemTipsViewTag];
  30. view.hidden = hidden;
  31. if (!hidden) {
  32. UILabel *label = [view viewWithTag:spsb_numberTipsViewLabelTag];
  33. label.text = tips;
  34. }
  35. }
  36. - (void)clickAction:(UIButton *)sender {
  37. SPSBTabBarType newTab = sender.tag - spsb_itemTag;
  38. if (newTab == _currentTab) {
  39. if (_spsb_tabbarDelegate && [_spsb_tabbarDelegate respondsToSelector:@selector(tabBar:clickSameType:)]) {
  40. [_spsb_tabbarDelegate tabBar:self clickSameType:_currentTab];
  41. }
  42. return;
  43. }
  44. UIButton *button = [self viewWithTag:_currentTab + spsb_itemTag];
  45. button.selected = false;
  46. _currentTab = sender.tag - spsb_itemTag;
  47. sender.selected = true;
  48. if (_spsb_tabbarDelegate && [_spsb_tabbarDelegate respondsToSelector:@selector(tabBar:clickType:)]) {
  49. [_spsb_tabbarDelegate tabBar:self clickType:_currentTab];
  50. }
  51. }
  52. - (void)setTabBarItems {
  53. NSArray *titleArray = @[@"首页", @"查询", @"咨询", @"我的"];
  54. NSArray *imageArray = @[
  55. jxh_getImage(home_btn_n),
  56. jxh_getImage(search_btn_n),
  57. jxh_getImage(consultation_btn_n),
  58. jxh_getImage(my_btn_n),
  59. jxh_getImage(home_btn_s),
  60. jxh_getImage(search_btn_s),
  61. jxh_getImage(consultation_btn_s),
  62. jxh_getImage(my_btn_s)
  63. ];
  64. NSMutableArray *buttonArray = [@[] mutableCopy];
  65. CGFloat buttonWidth = jxh_screenWidth() / titleArray.count;
  66. for (NSInteger i = 0; i < titleArray.count; i ++) {
  67. JXHCenterButton *button = [JXHCenterButton convenienceWithAxis:JXHCenterButtonAxisVertical interval:3 font:spsb_semiboldFont(10) target:self action:@selector(clickAction:)];
  68. [button setTitle:titleArray[i] titleColor:spsb_999999_color() state:JXHButtonControlStateNormal];
  69. [button setTitleColor:spsb_3296FB_color() state:JXHButtonControlStateSelected | JXHButtonControlStateHighlighted | JXHButtonControlStateSelectedAndHighlighted];
  70. [button setImage:imageArray[i] state:JXHButtonControlStateNormal];
  71. [button setImage:imageArray[i + titleArray.count] state:JXHButtonControlStateSelected | JXHButtonControlStateHighlighted | JXHButtonControlStateSelectedAndHighlighted];
  72. button.tag = spsb_itemTag + SPSBTabBarTypeHome + i;
  73. [self addSubview:button];
  74. [button makeConstraints:^(JXHConstraintMaker *make) {
  75. make.top.equalTo(7);
  76. make.width.equalTo(buttonWidth);
  77. make.leading.equalTo(buttonWidth * i);
  78. }];
  79. if (i == titleArray.count - 1) {
  80. UIView *tipsView = spsb_createNumberTipsViewWithTag(spsb_itemTipsViewTag);
  81. tipsView.hidden = true;
  82. [self addSubview:tipsView];
  83. [tipsView makeConstraints:^(JXHConstraintMaker *make) {
  84. make.leading.equalTo(button.centerX).offset(4);
  85. make.top.equalTo(4);
  86. }];
  87. } else {
  88. UIView *tips = UIView.new;
  89. tips.backgroundColor = spsb_FF5E5E_color();
  90. [tips setLayerCornerRadius:4 clipToBounds:false];
  91. tips.tag = spsb_itemTipsViewTag;
  92. tips.hidden = true;
  93. [self addSubview:tips];
  94. [tips makeConstraints:^(JXHConstraintMaker *make) {
  95. make.size.equalTo(CGSizeMake(8, 8));
  96. make.top.equalTo(5);
  97. make.centerX.equalTo(button.centerX).offset(15);
  98. }];
  99. [buttonArray addObject:button];
  100. }
  101. }
  102. _spsb_buttonArray = buttonArray;
  103. }
  104. @end
  105. #undef spsb_itemTag
  106. #undef spsb_itemIconTag
  107. #undef spsb_itemTipsViewTag