#import "ScrollMenuView.h" #import "YXMenuItemView.h" @interface ScrollMenuView() { CGFloat _itemSpace; } @property(nonatomic,strong) UIScrollView *menuScrollView; @property(nonatomic,strong) UIScrollView *contentView; @property(nonatomic,strong) NSMutableArray *menuBtnArray; @property (nonatomic, assign) NSInteger selectedIndex; @end @implementation ScrollMenuView -(instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { _itemSpace = 20; self.selectedIndex = 0; self.menuBtnArray = [[NSMutableArray alloc]init]; UIScrollView *menuScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 40)]; menuScrollView.delegate = self; menuScrollView.showsVerticalScrollIndicator = NO; menuScrollView.showsHorizontalScrollIndicator = NO; [self addSubview:menuScrollView]; self.menuScrollView = menuScrollView; UIScrollView *contentView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(menuScrollView.frame), frame.size.width, frame.size.height - CGRectGetHeight(menuScrollView.frame))]; contentView.delegate = self; contentView.showsVerticalScrollIndicator = NO; contentView.showsHorizontalScrollIndicator = NO; contentView.pagingEnabled = YES; [self addSubview:contentView]; self.contentView = contentView; } return self; } -(void)setScrollMenuView { [self.menuBtnArray removeAllObjects]; [self.menuScrollView removeAllSubviews]; [self.contentView removeAllSubviews]; if (self.titleArray.count > 0) { CGFloat allWidth = 0; for (int i = 0; i < self.titleArray.count; i++) { NSString *text = _titleArray[i]; YXMenuItemView *menuItemView = [[YXMenuItemView alloc]init]; NSDictionary *dict = @{NSFontAttributeName: menuItemView.textLabel.font}; CGSize textSize = [text boundingRectWithSize:CGSizeMake(200, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size; CGFloat width = textSize.width+2; [menuItemView.colorView mas_updateConstraints:^(MASConstraintMaker *make) { make.width.equalTo(@(width)); }]; menuItemView.frame = CGRectMake(i*_itemSpace + allWidth + 10, 0, width, 30); allWidth = allWidth + width; menuItemView.textLabel.text = text; if (i == self.selectedIndex) { menuItemView.colorView.hidden = NO; menuItemView.textLabel.textColor = [UIColor colorWithHexString:@"#333333"]; }else{ menuItemView.colorView.hidden = YES; menuItemView.textLabel.textColor = [UIColor colorWithHexString:@"#999999"]; } [self.menuScrollView addSubview:menuItemView]; menuItemView.tag = i; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(menuItemTapAction:)]; [menuItemView addGestureRecognizer:tap]; } self.menuScrollView.contentSize = CGSizeMake(_titleArray.count * _itemSpace + allWidth, 30); }else{ NSLog(@"--- titleArray为空 ---"); } if (self.subArray.count > 0) { self.contentView.contentSize = CGSizeMake(self.subArray.count * CGRectGetWidth(self.contentView.frame), CGRectGetHeight(self.contentView.frame)); for (int j = 0; j < self.subArray.count; j++) { UIView *tempView = self.subArray[j]; tempView.frame = CGRectMake(j * CGRectGetWidth(self.contentView.frame), 0, CGRectGetWidth(self.contentView.frame), CGRectGetHeight(self.contentView.frame)); [self.contentView addSubview:tempView]; } }else{ NSLog(@"--- subArray为空 ---"); } } -(void)menuItemTapAction:(UITapGestureRecognizer *)tap{ NSInteger tag = tap.view.tag; self.selectedIndex = tag; [self changeViewWithAnimate:self.selectedIndex]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if ([scrollView isEqual:self.contentView]) { NSLog(@"--------- %f",scrollView.contentOffset.x); int num = scrollView.contentOffset.x * pow(10, 6); int tempNum = SCREEN_WIDTH * pow(10, 6); if (num % tempNum != 0) { return; } int index = scrollView.contentOffset.x/SCREEN_WIDTH; NSLog(@"-----index---- %d",index); if ([self.delegate respondsToSelector:@selector(scrollMenuViewAtIndexPath:)]) { [self.delegate scrollMenuViewAtIndexPath:index]; } self.selectedIndex = index; [self changeViewWithAnimate:self.selectedIndex]; } } -(void)changeViewWithAnimate:(NSInteger)index { NSArray *subViews = self.menuScrollView.subviews; if (self.menuScrollView.subviews.count > index) { UIView *view = self.menuScrollView.subviews[index]; [self.menuScrollView scrollRectToVisible:view.frame animated:YES]; } for (int i = 0; i < self.menuScrollView.subviews.count; i++) { YXMenuItemView *menuItemView = self.menuScrollView.subviews[i]; if (i == index) { menuItemView.colorView.hidden = NO; menuItemView.textLabel.textColor = [UIColor colorWithHexString:@"#333333"]; }else{ menuItemView.colorView.hidden = YES; menuItemView.textLabel.textColor = [UIColor colorWithHexString:@"#999999"]; } } self.contentView.contentOffset = CGPointMake(index * CGRectGetWidth(self.contentView.frame), 0); if ([self.delegate respondsToSelector:@selector(scrollMenuViewAtIndexPath:)]) { [self.delegate scrollMenuViewAtIndexPath:index]; } } -(UIColor *)backColor { if (_backColor == nil) { self.menuScrollView.backgroundColor = [UIColor grayColor]; return [UIColor grayColor]; } self.menuScrollView.backgroundColor = _backColor; return _backColor; } -(UIColor *)normalColor { if (_normalColor == nil) { return [UIColor blackColor]; } return _normalColor; } -(UIColor *)selectColor { if (_selectColor == nil) { return [UIColor blueColor]; } return _selectColor; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end