MOSquareTopMenuView.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // MOSquareTopMenuView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2023/10/18.
  6. //
  7. #define TopBtnHeight 24.0
  8. #import "MOSquareTopMenuView.h"
  9. #import "UIView+Extension.h"
  10. @interface MOSquareTopMenuView ()
  11. @property (nonatomic, strong) UIImageView *lineView;
  12. @end
  13. @implementation MOSquareTopMenuView
  14. {
  15. /** 用于记录最后创建的控件 */
  16. UIView *_lastView;
  17. }
  18. #pragma mark - 重写构造方法
  19. /** 重写构造方法 */
  20. - (instancetype)initWithFrame:(CGRect)frame
  21. {
  22. if (self = [super initWithFrame:frame])
  23. {
  24. self.showsHorizontalScrollIndicator = NO;
  25. _currentButtonIndex = 1; // 默认当前选择的按钮是第二个
  26. }
  27. return self;
  28. }
  29. #pragma mark - 赋值标题数组
  30. /** 赋值标题数组 */
  31. - (void)setTitleArray:(NSArray *)titleArray{
  32. _titleArray = titleArray;
  33. // 先将所有子控件移除
  34. for (UIView *subView in self.subviews)
  35. {
  36. [subView removeFromSuperview];
  37. }
  38. // 将lastView置空
  39. _lastView = nil;
  40. // 遍历标题数组
  41. [_titleArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
  42. {
  43. UIButton *menuButton = [[UIButton alloc]init];
  44. [self addSubview:menuButton];
  45. if (_lastView)
  46. {
  47. menuButton.frame = CGRectMake(_lastView.maxX + 1.0, 0, 100, self.height);
  48. }
  49. else
  50. {
  51. menuButton.frame = CGRectMake(0, 0, 100, self.height);
  52. }
  53. menuButton.tag = 100 + idx;
  54. [menuButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
  55. if(self.titleColor){
  56. [menuButton setTitleColor:self.titleColor forState:UIControlStateNormal];
  57. }
  58. else{
  59. //默认
  60. [menuButton setTitleColor:[MOTools colorWithHexString:@"#120817" alpha:1.0] forState:UIControlStateNormal];
  61. }
  62. if(self.selectColor){
  63. [menuButton setTitleColor:self.selectColor forState:UIControlStateSelected];
  64. }
  65. else{
  66. [menuButton setTitleColor:[MOTools colorWithHexString:@"#E72DD3" alpha:1.0] forState:UIControlStateSelected];
  67. }
  68. [menuButton setTitle:obj forState:UIControlStateNormal];
  69. menuButton.backgroundColor = [UIColor clearColor];
  70. [menuButton addTarget:self action:@selector(menuButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  71. CGFloat width = [MOTools getWidthWithString:obj font:[UIFont systemFontOfSize:16.0]];
  72. // 宽度自适应
  73. [menuButton sizeToFit];
  74. menuButton.width = width + 10.0;
  75. menuButton.height = TopBtnHeight;
  76. // 这句不能少,不然初始化时button的label的宽度为0
  77. [menuButton layoutIfNeeded];
  78. // 默认第一个按钮时选中状态
  79. if (idx == 1)
  80. {
  81. menuButton.selected = YES;
  82. menuButton.backgroundColor = [UIColor clearColor];
  83. }
  84. if(idx == 0){
  85. [self addSubview:self.lineView];
  86. self.lineView.centerX = menuButton.centerX;
  87. }
  88. _lastView = menuButton;
  89. }];
  90. self.contentSize = CGSizeMake(CGRectGetMaxX(_lastView.frame), CGRectGetHeight(self.frame));
  91. }
  92. #pragma mark - 菜单按钮点击
  93. ///菜单按钮点击
  94. - (void)menuButtonClicked:(UIButton *)sender
  95. {
  96. // 改变按钮的选中状态
  97. for (UIButton *button in self.subviews)
  98. {
  99. if ([button isMemberOfClass:[UIButton class]])
  100. {
  101. button.selected = NO;
  102. [button.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
  103. }
  104. }
  105. sender.selected = YES;
  106. if(self.selectFont){
  107. [sender.titleLabel setFont:self.selectFont];
  108. }
  109. else{
  110. [sender.titleLabel setFont:[UIFont boldSystemFontOfSize:16.0]];
  111. }
  112. // 将所点击的button移到中间
  113. if (_lastView.maxX > self.width)
  114. {
  115. if (sender.x >= self.width / 2 && sender.centerX <= self.contentSize.width - self.width/2)
  116. {
  117. [UIView animateWithDuration:0.3 animations:^
  118. {
  119. self.contentOffset = CGPointMake(sender.centerX - self.width / 2, 0);
  120. self.lineView.centerX = sender.centerX;
  121. }];
  122. }
  123. else if (sender.frame.origin.x < self.width / 2)
  124. {
  125. [UIView animateWithDuration:0.3 animations:^
  126. {
  127. self.contentOffset = CGPointMake(0, 0);
  128. self.lineView.centerX = sender.centerX;
  129. }];
  130. }
  131. else
  132. {
  133. [UIView animateWithDuration:0.3 animations:^
  134. {
  135. self.contentOffset = CGPointMake(self.contentSize.width - self.width, 0);
  136. self.lineView.centerX = sender.centerX;
  137. }];
  138. }
  139. }
  140. else{
  141. [UIView animateWithDuration:0.3 animations:^
  142. {
  143. self.lineView.centerX = sender.centerX;
  144. }];
  145. }
  146. if (self.clickBtnBlock)
  147. {
  148. self.clickBtnBlock(sender.tag - 100);
  149. }
  150. }
  151. #pragma mark - 赋值当前选择的按钮
  152. /** 赋值当前选择的按钮 */
  153. - (void)setCurrentButtonIndex:(NSInteger)currentButtonIndex
  154. {
  155. _currentButtonIndex = currentButtonIndex;
  156. // 改变按钮的选中状态
  157. UIButton *currentButton = [self viewWithTag:(100 + currentButtonIndex)];
  158. for (UIButton *button in self.subviews)
  159. {
  160. if ([button isMemberOfClass:[UIButton class]])
  161. {
  162. button.selected = NO;
  163. [button.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
  164. }
  165. }
  166. currentButton.selected = YES;
  167. if(self.selectFont){
  168. [currentButton.titleLabel setFont:self.selectFont];
  169. }
  170. else{
  171. [currentButton.titleLabel setFont:[UIFont boldSystemFontOfSize:16.0]];
  172. }
  173. // 将所点击的button移到中间
  174. if (_lastView.maxX > self.width)
  175. {
  176. if (currentButton.x >= self.width / 2 && currentButton.centerX <= self.contentSize.width - self.width/2)
  177. {
  178. [UIView animateWithDuration:0.3 animations:^
  179. {
  180. self.contentOffset = CGPointMake(currentButton.centerX - self.width / 2, 0);
  181. self.lineView.centerX = currentButton.centerX;
  182. }];
  183. }
  184. else if (currentButton.x < self.width / 2)
  185. {
  186. [UIView animateWithDuration:0.3 animations:^
  187. {
  188. self.contentOffset = CGPointMake(0, 0);
  189. self.lineView.centerX = currentButton.centerX;
  190. }];
  191. }
  192. else
  193. {
  194. [UIView animateWithDuration:0.3 animations:^
  195. {
  196. self.contentOffset = CGPointMake(self.contentSize.width - self.width, 0);
  197. self.lineView.centerX = currentButton.centerX;
  198. }];
  199. }
  200. }
  201. else{
  202. [UIView animateWithDuration:0.3 animations:^
  203. {
  204. self.lineView.centerX = currentButton.centerX;
  205. }];
  206. }
  207. }
  208. - (void)setLineImg:(UIImage *)lineImg{
  209. _lineImg = lineImg;
  210. if(lineImg){
  211. [self.lineView setImage:lineImg];
  212. self.lineView.contentMode = UIViewContentModeTop;
  213. }
  214. }
  215. #pragma mark - Lazy
  216. - (UIImageView *)lineView{
  217. if (_lineView == nil){
  218. _lineView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, TopBtnHeight, 26.5, 6.5)];
  219. [_lineView setImage:[UIImage imageNamed:@"icon_square_line"]];
  220. }
  221. return _lineView;
  222. }
  223. @end