// // MONewSquareTopMenuView.m // MiMoLive // // Created by SuperC on 2024/7/18. // #define TopBtnHeight 24.0 #import "MONewSquareTopMenuView.h" #import "UIView+Extension.h" #import "MOThemeManager.h" @interface MONewSquareTopMenuView () @property (nonatomic, strong) UIImageView *lineView; @end @implementation MONewSquareTopMenuView { /** 用于记录最后创建的控件 */ UIView *_lastView; } #pragma mark - 重写构造方法 /** 重写构造方法 */ - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.showsHorizontalScrollIndicator = NO; _currentButtonIndex = 1; // 默认当前选择的按钮是第二个 self.selectFont = [UIFont fontWithName:kNormalTitleFontStr size:20.0]; if(!self.selectFont){ self.selectFont = [MOTextTools MODisplayFontWithSize:20.0 bold:YES itatic:YES weight:UIFontWeightMedium]; } } return self; } #pragma mark - 赋值标题数组 /** 赋值标题数组 */ - (void)setTitleArray:(NSArray *)titleArray{ _titleArray = titleArray; // 先将所有子控件移除 for (UIView *subView in self.subviews) { [subView removeFromSuperview]; } // 将lastView置空 _lastView = nil; // 遍历标题数组 [_titleArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { UIButton *menuButton = [[UIButton alloc]init]; [self addSubview:menuButton]; if (_lastView) { menuButton.frame = CGRectMake(_lastView.maxX + 1.0, 0, 100, self.height); } else { menuButton.frame = CGRectMake(0, 0, 100, self.height); } MOLogV(@"%.f",self.height); menuButton.tag = 100 + idx; UIFont *customFont = [UIFont fontWithName:kNormalTitleFontStr size:16.0]; if(!customFont){ customFont = [MOTextTools MODisplayFontWithSize:16.0 bold:YES itatic:YES weight:UIFontWeightMedium]; } [menuButton.titleLabel setFont:customFont]; if(self.titleColor){ [menuButton setTitleColor:self.titleColor forState:UIControlStateNormal]; } else{ //默认 [menuButton setTitleColor:[MOTools colorWithHexString:@"#8E8A99" alpha:1.0] forState:UIControlStateNormal]; } if(self.selectColor){ [menuButton setTitleColor:self.selectColor forState:UIControlStateSelected]; } else{ [menuButton setTitleColor:[MOTools colorWithHexString:@"#000000" alpha:1.0] forState:UIControlStateSelected]; } [menuButton setTitle:obj forState:UIControlStateNormal]; menuButton.backgroundColor = [UIColor clearColor]; [menuButton addTarget:self action:@selector(menuButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; CGFloat width = [MOTools getWidthWithString:obj font:self.selectFont]; // 宽度自适应 [menuButton sizeToFit]; menuButton.width = width + 10.0; menuButton.height = TopBtnHeight; // 这句不能少,不然初始化时button的label的宽度为0 [menuButton layoutIfNeeded]; // 默认第一个按钮时选中状态 if (idx == 1) { menuButton.selected = YES; menuButton.backgroundColor = [UIColor clearColor]; } if(idx == 0){ [self addSubview:self.lineView]; [self sendSubviewToBack:self.lineView]; self.lineView.centerX = menuButton.centerX; } _lastView = menuButton; }]; self.contentSize = CGSizeMake(CGRectGetMaxX(_lastView.frame), CGRectGetHeight(self.frame)); } #pragma mark - 菜单按钮点击 ///菜单按钮点击 - (void)menuButtonClicked:(UIButton *)sender { UIFont *noSelectCustomFont = [UIFont fontWithName:kNormalTitleFontStr size:16.0]; if(!noSelectCustomFont){ noSelectCustomFont = [MOTextTools MODisplayFontWithSize:16.0 bold:YES itatic:YES weight:UIFontWeightMedium]; } // 改变按钮的选中状态 for (UIButton *button in self.subviews) { if ([button isMemberOfClass:[UIButton class]]) { button.selected = NO; [button.titleLabel setFont:noSelectCustomFont]; } } sender.selected = YES; if(self.selectFont){ [sender.titleLabel setFont:self.selectFont]; } else{ [sender.titleLabel setFont:noSelectCustomFont]; } // 将所点击的button移到中间 if (_lastView.maxX > self.width) { if (sender.x >= self.width / 2 && sender.centerX <= self.contentSize.width - self.width/2) { [UIView animateWithDuration:0.3 animations:^ { self.contentOffset = CGPointMake(sender.centerX - self.width / 2, 0); self.lineView.centerX = sender.centerX; }]; } else if (sender.frame.origin.x < self.width / 2) { [UIView animateWithDuration:0.3 animations:^ { self.contentOffset = CGPointMake(0, 0); self.lineView.centerX = sender.centerX; }]; } else { [UIView animateWithDuration:0.3 animations:^ { self.contentOffset = CGPointMake(self.contentSize.width - self.width, 0); self.lineView.centerX = sender.centerX; }]; } } else{ [UIView animateWithDuration:0.3 animations:^ { self.lineView.centerX = sender.centerX; }]; } if (self.clickBtnBlock) { self.clickBtnBlock(sender.tag - 100); } } #pragma mark - 赋值当前选择的按钮 /** 赋值当前选择的按钮 */ - (void)setCurrentButtonIndex:(NSInteger)currentButtonIndex { _currentButtonIndex = currentButtonIndex; UIFont *noSelectCustomFont = [UIFont fontWithName:kNormalTitleFontStr size:16.0]; if(!noSelectCustomFont){ noSelectCustomFont = [MOTextTools MODisplayFontWithSize:16.0 bold:YES itatic:YES weight:UIFontWeightMedium]; } // 改变按钮的选中状态 UIButton *currentButton = [self viewWithTag:(100 + currentButtonIndex)]; for (UIButton *button in self.subviews) { if ([button isMemberOfClass:[UIButton class]]) { button.selected = NO; [button.titleLabel setFont:noSelectCustomFont]; } } currentButton.selected = YES; if(self.selectFont){ [currentButton.titleLabel setFont:self.selectFont]; } else{ [currentButton.titleLabel setFont:noSelectCustomFont]; } // 将所点击的button移到中间 if (_lastView.maxX > self.width) { if (currentButton.x >= self.width / 2 && currentButton.centerX <= self.contentSize.width - self.width/2) { [UIView animateWithDuration:0.3 animations:^ { self.contentOffset = CGPointMake(currentButton.centerX - self.width / 2, 0); self.lineView.centerX = currentButton.centerX; }]; } else if (currentButton.x < self.width / 2) { [UIView animateWithDuration:0.3 animations:^ { self.contentOffset = CGPointMake(0, 0); self.lineView.centerX = currentButton.centerX; }]; } else { [UIView animateWithDuration:0.3 animations:^ { self.contentOffset = CGPointMake(self.contentSize.width - self.width, 0); self.lineView.centerX = currentButton.centerX; }]; } } else{ [UIView animateWithDuration:0.3 animations:^ { self.lineView.centerX = currentButton.centerX; }]; } } - (void)setLineImg:(UIImage *)lineImg{ _lineImg = lineImg; if(lineImg){ [self.lineView setImage:lineImg]; self.lineView.contentMode = UIViewContentModeTop; } } #pragma mark - Lazy - (UIImageView *)lineView{ if (_lineView == nil){ _lineView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 15.0, 50.0, 8.5)]; [_lineView setImage:[UIImage imageNamed:@"icon_square_line_new"]]; if ([MOThemeManager shareManager].canUseTheme && [MOThemeManager shareManager].themeModel.titleBottom.length > 0) { [_lineView sd_setImageWithURL:[NSURL URLWithString:[MOThemeManager shareManager].themeModel.titleBottom]]; } } return _lineView; } @end