MOCustomTabBarView.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // MOCustomTabBarView.m
  3. // MiMoLive
  4. //
  5. // Created by MiMo on 2025/6/17.
  6. //
  7. #import "MOCustomTabBarView.h"
  8. #import "MOTabBarButton.h"
  9. @interface MOCustomTabBarView ()
  10. @property (nonatomic, strong) UIView *backgroundView;
  11. @property (nonatomic, strong) NSArray<MOTabBarButton *> *buttons;
  12. @property (nonatomic, assign) NSInteger currentIndex;
  13. @property (nonatomic, strong) UILabel *badgeLab;
  14. @end
  15. @implementation MOCustomTabBarView
  16. - (instancetype)initWithFrame:(CGRect)frame {
  17. if (self = [super initWithFrame:frame]) {
  18. [self setupUI];
  19. }
  20. return self;
  21. }
  22. - (void)setupUI {
  23. self.backgroundColor = UIColor.clearColor;
  24. // 圆角白底背景
  25. self.backgroundView = [[UIView alloc] init];
  26. self.backgroundView.backgroundColor = [UIColor whiteColor];
  27. self.backgroundView.layer.cornerRadius = 28;
  28. [self addSubview:self.backgroundView];
  29. // Shadow effect for backgroundView
  30. self.backgroundView.layer.shadowColor = [MOTools colorWithHexString:@"#404780" alpha:0.1].CGColor;
  31. self.backgroundView.layer.shadowOpacity = 1.0; // Full opacity for the shadow color
  32. self.backgroundView.layer.shadowOffset = CGSizeMake(0, 2); // Offset (X: 0pt, Y: 2pt)
  33. self.backgroundView.layer.shadowRadius = 4; // Blur radius (4pt)
  34. self.backgroundView.layer.masksToBounds = NO; // Allow shadow to be visible outside bounds
  35. // 图标按钮(4 个)
  36. NSArray *icons = @[@"icon_tabbar_home", @"icon_tabbar_explore", @"icon_tabbar_message", @"icon_tabbar_mine"];
  37. NSMutableArray *btns = [NSMutableArray array];
  38. for (NSInteger i = 0; i < icons.count; i++) {
  39. MOTabBarButton *btn = [MOTabBarButton buttonWithType:UIButtonTypeCustom];
  40. [btn setImage:[UIImage imageNamed:icons[i]] forState:UIControlStateNormal];
  41. [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_selected", icons[i]]] forState:UIControlStateSelected];
  42. btn.tag = i;
  43. [btn addTarget:self action:@selector(onTabClicked:) forControlEvents:UIControlEventTouchUpInside];
  44. [self addSubview:btn];
  45. [btns addObject:btn];
  46. if (i == 0) {//默认选中第一个
  47. btn.selected = YES;
  48. }
  49. }
  50. self.buttons = btns;
  51. }
  52. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  53. if (self.isHidden || self.alpha < 0.01 || !self.userInteractionEnabled) {
  54. return nil;
  55. }
  56. CGRect expandedBounds = CGRectInset(self.bounds, 0, -20);
  57. if (CGRectContainsPoint(expandedBounds, point)) {
  58. for (UIView *subview in self.subviews.reverseObjectEnumerator) {
  59. CGPoint convertedPoint = [self convertPoint:point toView:subview];
  60. UIView *hitView = [subview hitTest:convertedPoint withEvent:event];
  61. if (hitView) {
  62. return hitView;
  63. }
  64. }
  65. return self;
  66. }
  67. return nil;
  68. }
  69. - (void)layoutSubviews {
  70. [super layoutSubviews];
  71. CGFloat barHeight = 80.0 + kBottomSafeAreaInset;
  72. self.frame = CGRectMake(0, self.superview.bounds.size.height - barHeight, SCREENWIDTH, barHeight);
  73. // 白色背景容器
  74. self.backgroundView.frame = CGRectMake(24, 16, SCREENWIDTH - 48, 56);
  75. // 平均分布按钮
  76. CGFloat itemWidth = self.backgroundView.bounds.size.width / self.buttons.count;
  77. for (NSInteger i = 0; i < self.buttons.count; i++) {
  78. MOTabBarButton *btn = self.buttons[i];
  79. btn.frame = CGRectMake(CGRectGetMinX(self.backgroundView.frame) + i * itemWidth,
  80. CGRectGetMinY(self.backgroundView.frame),
  81. itemWidth,
  82. 56);
  83. if(i == 2){//IM未读数
  84. self.badgeLab.frame = CGRectMake(0, 23, self.badgeLab.width, 16);
  85. self.badgeLab.centerX = btn.centerX + 10;
  86. [self addSubview:self.badgeLab];
  87. }
  88. }
  89. }
  90. - (void)onTabClicked:(UIButton *)sender {
  91. [self setSelectedIndex:sender.tag];
  92. if ([self.delegate respondsToSelector:@selector(customTabBarDidSelectIndex:)]) {
  93. [self.delegate customTabBarDidSelectIndex:sender.tag];
  94. }
  95. }
  96. - (void)setSelectedIndex:(NSInteger)index {
  97. self.currentIndex = index;
  98. for (NSInteger i = 0; i < self.buttons.count; i++) {
  99. self.buttons[i].selected = (i == index);
  100. }
  101. }
  102. - (void)setIMUnreadMsgCount:(NSString *)badgeString {
  103. self.badgeLab.text = badgeString;
  104. if ([badgeString isEqualToString:@""]) {
  105. self.badgeLab.hidden = YES;
  106. } else {
  107. self.badgeLab.hidden = NO;
  108. }
  109. if ([badgeString isEqualToString:@"99+"]) {
  110. self.badgeLab.width = 20;
  111. } else {
  112. self.badgeLab.width = 16;
  113. }
  114. _badgeValue = badgeString;
  115. }
  116. - (UILabel *)badgeLab {
  117. if (!_badgeLab) {
  118. _badgeLab = [[UILabel alloc] init];
  119. _badgeLab.backgroundColor = [MOTools colorWithHexString:@"#FB5374" alpha:1.0];
  120. _badgeLab.font = [MOTextTools getTheFontWithSize:10.0 AndFontName:kNormalContentFontStr];
  121. _badgeLab.textColor = [UIColor whiteColor];
  122. _badgeLab.layer.cornerRadius = 8.0;
  123. _badgeLab.layer.masksToBounds = YES;
  124. _badgeLab.textAlignment = NSTextAlignmentCenter;
  125. }
  126. return _badgeLab;
  127. }
  128. @end