MONewSquareTopMenuView.m 8.6 KB

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