BGTabBar.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // BGTabBar.m
  3. // BuguLive
  4. //
  5. // Created by xfg on 2017/6/26.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "BGTabBar.h"
  9. @interface BGTabBar()
  10. @property (nonatomic, weak) QMUIButton *centerBtn;
  11. @end
  12. @implementation BGTabBar
  13. - (instancetype)initWithFrame:(CGRect)frame
  14. {
  15. if (self = [super initWithFrame:frame])
  16. {
  17. [self setupInit];
  18. }
  19. return self;
  20. }
  21. - (void)setupInit
  22. {
  23. // if (!isIPhoneX())
  24. // {
  25. // // 设置样式 取出边线
  26. // self.barStyle = UIBarStyleBlack;
  27. // UIImage *image = [UIImage imageNamed:@"ic_tab_bg"];
  28. // self.backgroundImage = image;
  29. // if (kScreenW > 375) {
  30. // self.backgroundImage = [self scaleToSize:image size:CGSizeMake(kScreenW, kScreenW * 57 / 375)];
  31. // }
  32. //
  33. // }
  34. // else
  35. // {
  36. // self.backgroundColor = [UIColor whiteColor];
  37. // self.tintColor = [UIColor whiteColor];
  38. // self.translucent = NO;
  39. // }
  40. // UIImageView * backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, self.height)];
  41. // backImageView.image = [UIImage imageNamed:@"ic_tab_bg"];
  42. // [self addSubview:backImageView];
  43. }
  44. - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)newsize{
  45. // 创建一个bitmap的context
  46. // 并把它设置成为当前正在使用的context
  47. UIGraphicsBeginImageContext(newsize);
  48. // 绘制改变大小的图片
  49. [img drawInRect:CGRectMake(0, 0, newsize.width, newsize.height)];
  50. // 从当前context中创建一个改变大小后的图片
  51. UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  52. // 使当前的context出堆栈
  53. UIGraphicsEndImageContext();
  54. // 返回新的改变大小后的图片
  55. return scaledImage;
  56. }
  57. - (void)centerButtonClicked
  58. {
  59. if (self.centerBtnClickBlock)
  60. {
  61. self.centerBtnClickBlock();
  62. }
  63. }
  64. #pragma mark 布局子控件
  65. - (void)layoutSubviews
  66. {
  67. [super layoutSubviews];
  68. NSInteger count = self.items.count;
  69. //确定单个控件大小
  70. CGFloat buttonW = self.frame.size.width / (count + 1);
  71. CGFloat buttonH = 49;
  72. CGFloat tabBarBtnY = 0;
  73. int tabBarBtnIndex = 0;
  74. for (UIView *subView in self.subviews)
  75. {
  76. if ([subView isKindOfClass:NSClassFromString(@"UITabBarButton")])
  77. {
  78. if (tabBarBtnIndex == count / 2)
  79. {
  80. tabBarBtnIndex ++;
  81. }
  82. CGFloat btnX = tabBarBtnIndex * buttonW;
  83. subView.frame = CGRectMake(btnX, tabBarBtnY, buttonW, buttonH);
  84. tabBarBtnIndex ++;
  85. UIControl *tabBarBtn = (UIControl *)subView;
  86. [tabBarBtn addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  87. }
  88. }
  89. /**** 设置中间按钮frame ****/
  90. self.centerBtn.centerX = self.width * 0.5;
  91. #if kSupportH5Shopping
  92. self.centerBtn.y = buttonH - self.centerBtn.height;
  93. #else
  94. self.centerBtn.centerY = buttonH / 2;
  95. #endif
  96. }
  97. #pragma mark 动画
  98. - (void)tabBarButtonClick:(UIControl *)tabBarButton
  99. {
  100. for (UIView *imageView in tabBarButton.subviews)
  101. {
  102. if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")])
  103. {
  104. // 需要实现的帧动画,这里根据需求自定义
  105. CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
  106. animation.keyPath = @"transform.scale";
  107. // @1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0
  108. animation.values = @[@1.0,@1.25,@0.9,@1.15,@1.0];
  109. animation.duration = 0.4;
  110. animation.calculationMode = kCAAnimationCubic;
  111. // 把动画添加上去就OK了
  112. [imageView.layer addAnimation:animation forKey:nil];
  113. }
  114. }
  115. }
  116. #pragma mark 设置允许交互的区域 方法返回的view为处理事件最合适的view
  117. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
  118. {
  119. UIView *view = [super hitTest:point withEvent:event];
  120. if (!self.isHidden)
  121. {
  122. //转换坐标到中间按钮,生成一个新的点
  123. CGPoint pointInCenterBtn = [self convertPoint:point toView:self.centerBtn];
  124. //判断 如果该点是在中间按钮,那么处理事件最合适的View,就是这个button
  125. if ([self.centerBtn pointInside:pointInCenterBtn withEvent:event])
  126. {
  127. return self.centerBtn;
  128. }
  129. return view;
  130. }
  131. return view;
  132. }
  133. #pragma mark - lazy
  134. - (QMUIButton *)centerBtn
  135. {
  136. if (!_centerBtn)
  137. {
  138. QMUIButton *centerButton = [QMUIButton buttonWithType:UIButtonTypeCustom];
  139. #if kSupportH5Shopping
  140. [centerButton setImage:[UIImage imageNamed:@"ic_H5_tab_live"] forState:UIControlStateNormal];
  141. [centerButton setImage:[UIImage imageNamed:@"ic_H5_tab_live"] forState:UIControlStateHighlighted];
  142. #else
  143. [centerButton setImage:[UIImage imageNamed:@"ic_live_tab_create_live_normal"] forState:UIControlStateNormal];
  144. [centerButton setImage:[UIImage imageNamed:@"ic_live_tab_create_live_normal"] forState:UIControlStateHighlighted];
  145. #endif
  146. [centerButton addTarget:self action:@selector(centerButtonClicked) forControlEvents:UIControlEventTouchUpInside];
  147. centerButton.titleLabel.font = [UIFont systemFontOfSize:14];
  148. [centerButton setTitle:ASLocalizedString(@"")forState:UIControlStateNormal];
  149. centerButton.imagePosition = QMUIButtonImagePositionTop;
  150. // centerButton setTitleColor:<#(nullable UIColor *)#> forState:<#(UIControlState)#>
  151. centerButton.spacingBetweenImageAndTitle = 5;
  152. [centerButton sizeToFit];
  153. [self addSubview:centerButton];
  154. _centerBtn = centerButton;
  155. }
  156. return _centerBtn;
  157. }
  158. @end