123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- //
- // 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
|