// // ZCNavMenuView.m // LiveChat // // Created by 张灿 on 2018/9/7. // Copyright © 2018年 caicai. All rights reserved. // #import "ZCNavMenuView.h" @interface ZCNavMenuView() { NSInteger _selectIndex;//用来设置tag的基准值 记录当前的按钮 NSArray *_titleArr; UIFont* _normalFont; UIColor* _normalColor; UIColor* _selectColor; UIFont* _selectFont; UIColor* _selectLineColor; BOOL _isBackground; } @end @implementation ZCNavMenuView - (instancetype)initWithFrame:(CGRect)frame TitleArr:(NSArray *)arr normalFont:(UIFont*)font normalColor:(UIColor*)normalColor selectFont:(UIFont*)selectFont selectColor:(UIColor*)selectcolor selectLineColor:(UIColor*)selectLineColor currentIndex:(NSInteger)currentIndex isBackground:(BOOL)isBackground{ if (self = [super initWithFrame:frame]) { _selectIndex = currentIndex+1000; _titleArr = arr; _normalFont = font; _normalColor = normalColor; _selectFont = selectFont; _selectColor = selectcolor; _selectLineColor = selectLineColor; _isBackground = isBackground; self.frame = frame; self.backgroundColor = [UIColor clearColor]; if (isBackground) { [self youpaifsetupView1]; }else{ [self youpaifsetupView]; } } return self; } -(void)setBgColor:(UIColor *)bgColor{ _bgColor = bgColor; if (self.bgColor) { self.backgroundColor = self.bgColor; }else{ self.backgroundColor = [UIColor clearColor]; } } - (void)youpaifsetupView1{ CGFloat width = self.frame.size.width/_titleArr.count; _selectView = [[UIView alloc]initWithFrame:CGRectMake((_selectIndex-1000)*width,0,width, self.frame.size.height)]; _selectView.backgroundColor = _selectLineColor; _selectView.layer.cornerRadius = 14; [self addSubview:_selectView]; //添加按钮 for (int i = 0; i<_titleArr.count; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(i*self.frame.size.width/_titleArr.count, 0, self.frame.size.width/_titleArr.count, self.frame.size.height); [button setTitle:_titleArr[i] forState:UIControlStateNormal]; [button setTitleColor:_normalColor forState:UIControlStateNormal]; button.titleLabel.font = _normalFont; if (_selectIndex-1000==i) { button.titleLabel.font = _selectFont; [button setTitleColor:_selectColor forState:UIControlStateNormal]; } button.tag = 1000+i; if (_selectIndex==button.tag) button.selected = YES; [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; } } - (void)youpaifsetupView{ //添加按钮 NSLog(@"%ld",_titleArr.count); NSLog(@"%ld",_selectIndex-1000); for (int i = 0; i<_titleArr.count; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(i*self.frame.size.width/_titleArr.count, 0, self.frame.size.width/_titleArr.count, self.frame.size.height); [button setTitle:_titleArr[i] forState:UIControlStateNormal]; [button setTitleColor:_normalColor forState:UIControlStateNormal]; button.titleLabel.font = _normalFont; if (_selectIndex-1000 == i) { button.titleLabel.font = _selectFont; [button setTitleColor:_selectColor forState:UIControlStateNormal]; } button.tag = 1000+i; if (_selectIndex==button.tag) button.selected = YES; [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; } // CGFloat initX = (self.frame.size.width/_titleArr.count-20)/2; // _selectView = [[UIView alloc]initWithFrame:CGRectMake(initX+(_selectIndex-1000)*self.frame.size.width/_titleArr.count, self.frame.size.height-6, 20, 2)]; // _selectView.backgroundColor = _selectLineColor; // _selectView.layer.cornerRadius = 1.0; // [self addSubview:_selectView]; CGFloat initX = (self.frame.size.width/_titleArr.count-20)/2; _selectView = [[UIView alloc]initWithFrame:CGRectMake(initX+(_selectIndex-1000)*self.frame.size.width/_titleArr.count, self.frame.size.height-6, 20, 2)]; _selectView.backgroundColor = _selectLineColor; _selectView.layer.cornerRadius = 1.0; [self addSubview:_selectView]; } - (void)btnClick:(UIButton *)button{ if (button.tag==_selectIndex) return; UIButton *fore = (UIButton *)[self viewWithTag:_selectIndex]; // fore.selected = NO; fore.titleLabel.font = _normalFont; [fore setTitleColor:_normalColor forState:UIControlStateNormal]; // button.selected = YES; button.titleLabel.font = _selectFont; [button setTitleColor:_selectColor forState:UIControlStateNormal]; _selectIndex = button.tag; [UIView animateWithDuration:0.25 animations:^{ if (self->_isBackground) { self->_selectView.frame = CGRectMake((self->_selectIndex-1000)*self.frame.size.width/self->_titleArr.count,0,self.frame.size.width/self->_titleArr.count, self.frame.size.height); }else{ self->_selectView.frame = CGRectMake((self.frame.size.width/self->_titleArr.count-20)/2.0+(self->_selectIndex-1000)*self.frame.size.width/self->_titleArr.count, self.frame.size.height-6, 20, 2); } }]; if (self.delegate&&[self.delegate respondsToSelector:@selector(youpaifnavBtnClick:)]) { [self.delegate youpaifnavBtnClick:(button.tag-1000)]; } } - (void)changeCurrentSelectButton:(NSInteger)index{ NSLog(@"22%d",_isBackground); if (index+1000==_selectIndex) return; UIButton *fore = (UIButton *)[self viewWithTag:_selectIndex]; // fore.selected = NO; fore.titleLabel.font = _normalFont; [fore setTitleColor:_normalColor forState:UIControlStateNormal]; _selectIndex = index+1000; UIButton *behind = (UIButton *)[self viewWithTag:_selectIndex]; // behind.selected = YES; behind.titleLabel.font = _selectFont; [behind setTitleColor:_selectColor forState:UIControlStateNormal]; [UIView animateWithDuration:0.25 animations:^{ if (self->_isBackground) { self->_selectView.frame = CGRectMake((self->_selectIndex-1000)*self.frame.size.width/self->_titleArr.count,0,self.frame.size.width/self->_titleArr.count, self.frame.size.height); }else{ self->_selectView.frame = CGRectMake((self.frame.size.width/self->_titleArr.count-20)/2+(self->_selectIndex-1000)*self.frame.size.width/self->_titleArr.count, self.frame.size.height-6, 20, 2); } }]; } -(instancetype)initWithFrame:(CGRect)frame TitleArr:(NSArray *)arr normalFont:(UIFont *)font normalColor:(UIColor *)normalColor selectFont:(UIFont *)selectFont selectColor:(UIColor *)selectcolor selectLineColor:(UIColor *)selectLineColor currentIndex:(NSInteger)currentIndex { if (self = [super initWithFrame:frame]) { NSLog(@"11111 === %ld",currentIndex); _selectIndex = currentIndex+1000; _titleArr = arr; _normalFont = font; _normalColor = normalColor; _selectFont = selectFont; _selectColor = selectcolor; _selectLineColor = selectLineColor; self.frame = frame; self.backgroundColor = [UIColor clearColor]; [self youpaifsetupView2]; } return self; } - (void)youpaifsetupView2{ //添加按钮 NSLog(@"22222 === %d",_selectIndex-1000); for (int i = 0; i<_titleArr.count; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(i*self.frame.size.width/_titleArr.count, 0, self.frame.size.width/_titleArr.count, self.frame.size.height); [button setTitle:_titleArr[i] forState:UIControlStateNormal]; [button setTitleColor:_normalColor forState:UIControlStateNormal]; button.titleLabel.font = _normalFont; if (_selectIndex-1000 == i) { button.titleLabel.font = _selectFont; [button setTitleColor:_selectColor forState:UIControlStateNormal]; } button.tag = 1000+i; if (_selectIndex==button.tag) button.selected = YES; [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; if (_selectIndex -1000 == 0) { [button sendActionsForControlEvents:UIControlEventTouchUpInside]; } } CGFloat initX = (self.frame.size.width/_titleArr.count-20)/2; _selectView = [[UIView alloc]initWithFrame:CGRectMake(initX+(_selectIndex-1000)*self.frame.size.width/_titleArr.count, self.frame.size.height-6, 20, 2)]; _selectView.backgroundColor = _selectLineColor; _selectView.layer.cornerRadius = 1.0; [self addSubview:_selectView]; } @end