| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- //
- // MOSquareTopMenuView.m
- // MiMoLive
- //
- // Created by SuperC on 2023/10/18.
- //
- #define TopBtnHeight 24.0
- #import "MOSquareTopMenuView.h"
- #import "UIView+Extension.h"
- @interface MOSquareTopMenuView ()
- @property (nonatomic, strong) UIImageView *lineView;
- @end
- @implementation MOSquareTopMenuView
- {
- /** 用于记录最后创建的控件 */
- UIView *_lastView;
- }
- #pragma mark - 重写构造方法
- /** 重写构造方法 */
- - (instancetype)initWithFrame:(CGRect)frame
- {
- if (self = [super initWithFrame:frame])
- {
- self.showsHorizontalScrollIndicator = NO;
- _currentButtonIndex = 1; // 默认当前选择的按钮是第二个
- }
- 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);
- }
-
- menuButton.tag = 100 + idx;
- [menuButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
-
- if(self.titleColor){
- [menuButton setTitleColor:self.titleColor forState:UIControlStateNormal];
- }
- else{
- //默认
- [menuButton setTitleColor:[MOTools colorWithHexString:@"#120817" alpha:1.0] forState:UIControlStateNormal];
- }
-
- if(self.selectColor){
- [menuButton setTitleColor:self.selectColor forState:UIControlStateSelected];
- }
- else{
- [menuButton setTitleColor:[MOTools colorWithHexString:@"#E72DD3" 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:[UIFont systemFontOfSize:16.0]];
-
- // 宽度自适应
- [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.lineView.centerX = menuButton.centerX;
- }
-
- _lastView = menuButton;
- }];
-
- self.contentSize = CGSizeMake(CGRectGetMaxX(_lastView.frame), CGRectGetHeight(self.frame));
- }
- #pragma mark - 菜单按钮点击
- ///菜单按钮点击
- - (void)menuButtonClicked:(UIButton *)sender
- {
- // 改变按钮的选中状态
- for (UIButton *button in self.subviews)
- {
- if ([button isMemberOfClass:[UIButton class]])
- {
- button.selected = NO;
- [button.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
- }
- }
- sender.selected = YES;
-
- if(self.selectFont){
- [sender.titleLabel setFont:self.selectFont];
- }
- else{
- [sender.titleLabel setFont:[UIFont boldSystemFontOfSize:16.0]];
- }
-
- // 将所点击的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;
-
- // 改变按钮的选中状态
- UIButton *currentButton = [self viewWithTag:(100 + currentButtonIndex)];
- for (UIButton *button in self.subviews)
- {
- if ([button isMemberOfClass:[UIButton class]])
- {
- button.selected = NO;
- [button.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
- }
- }
- currentButton.selected = YES;
- if(self.selectFont){
- [currentButton.titleLabel setFont:self.selectFont];
- }
- else{
- [currentButton.titleLabel setFont:[UIFont boldSystemFontOfSize:16.0]];
- }
-
-
- // 将所点击的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, TopBtnHeight, 26.5, 6.5)];
- [_lineView setImage:[UIImage imageNamed:@"icon_square_line"]];
-
- }
-
- return _lineView;
- }
- @end
|