ZCNavMenuView.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // ZCNavMenuView.m
  3. // LiveChat
  4. //
  5. // Created by 张灿 on 2018/9/7.
  6. // Copyright © 2018年 caicai. All rights reserved.
  7. //
  8. #import "ZCNavMenuView.h"
  9. @interface ZCNavMenuView()
  10. {
  11. NSInteger _selectIndex;//用来设置tag的基准值 记录当前的按钮
  12. NSArray *_titleArr;
  13. UIFont* _normalFont;
  14. UIColor* _normalColor;
  15. UIColor* _selectColor;
  16. UIFont* _selectFont;
  17. UIColor* _selectLineColor;
  18. BOOL _isBackground;
  19. }
  20. @end
  21. @implementation ZCNavMenuView
  22. - (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{
  23. if (self = [super initWithFrame:frame]) {
  24. _selectIndex = currentIndex+1000;
  25. _titleArr = arr;
  26. _normalFont = font;
  27. _normalColor = normalColor;
  28. _selectFont = selectFont;
  29. _selectColor = selectcolor;
  30. _selectLineColor = selectLineColor;
  31. _isBackground = isBackground;
  32. self.frame = frame;
  33. self.backgroundColor = [UIColor clearColor];
  34. if (isBackground) {
  35. [self youpaifsetupView1];
  36. }else{
  37. [self youpaifsetupView];
  38. }
  39. }
  40. return self;
  41. }
  42. -(void)setBgColor:(UIColor *)bgColor{
  43. _bgColor = bgColor;
  44. if (self.bgColor) {
  45. self.backgroundColor = self.bgColor;
  46. }else{
  47. self.backgroundColor = [UIColor clearColor];
  48. }
  49. }
  50. - (void)youpaifsetupView1{
  51. CGFloat width = self.frame.size.width/_titleArr.count;
  52. _selectView = [[UIView alloc]initWithFrame:CGRectMake((_selectIndex-1000)*width,0,width, self.frame.size.height)];
  53. _selectView.backgroundColor = _selectLineColor;
  54. _selectView.layer.cornerRadius = 14;
  55. [self addSubview:_selectView];
  56. //添加按钮
  57. for (int i = 0; i<_titleArr.count; i++) {
  58. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  59. button.frame = CGRectMake(i*self.frame.size.width/_titleArr.count, 0, self.frame.size.width/_titleArr.count, self.frame.size.height);
  60. [button setTitle:_titleArr[i] forState:UIControlStateNormal];
  61. [button setTitleColor:_normalColor forState:UIControlStateNormal];
  62. button.titleLabel.font = _normalFont;
  63. if (_selectIndex-1000==i) {
  64. button.titleLabel.font = _selectFont;
  65. [button setTitleColor:_selectColor forState:UIControlStateNormal];
  66. }
  67. button.tag = 1000+i;
  68. if (_selectIndex==button.tag) button.selected = YES;
  69. [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  70. [self addSubview:button];
  71. }
  72. }
  73. - (void)youpaifsetupView{
  74. //添加按钮
  75. NSLog(@"%ld",_titleArr.count);
  76. NSLog(@"%ld",_selectIndex-1000);
  77. for (int i = 0; i<_titleArr.count; i++) {
  78. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  79. button.frame = CGRectMake(i*self.frame.size.width/_titleArr.count, 0, self.frame.size.width/_titleArr.count, self.frame.size.height);
  80. [button setTitle:_titleArr[i] forState:UIControlStateNormal];
  81. [button setTitleColor:_normalColor forState:UIControlStateNormal];
  82. button.titleLabel.font = _normalFont;
  83. if (_selectIndex-1000 == i) {
  84. button.titleLabel.font = _selectFont;
  85. [button setTitleColor:_selectColor forState:UIControlStateNormal];
  86. }
  87. button.tag = 1000+i;
  88. if (_selectIndex==button.tag) button.selected = YES;
  89. [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  90. [self addSubview:button];
  91. }
  92. // CGFloat initX = (self.frame.size.width/_titleArr.count-20)/2;
  93. // _selectView = [[UIView alloc]initWithFrame:CGRectMake(initX+(_selectIndex-1000)*self.frame.size.width/_titleArr.count, self.frame.size.height-6, 20, 2)];
  94. // _selectView.backgroundColor = _selectLineColor;
  95. // _selectView.layer.cornerRadius = 1.0;
  96. // [self addSubview:_selectView];
  97. CGFloat initX = (self.frame.size.width/_titleArr.count-20)/2;
  98. _selectView = [[UIView alloc]initWithFrame:CGRectMake(initX+(_selectIndex-1000)*self.frame.size.width/_titleArr.count, self.frame.size.height-6, 20, 2)];
  99. _selectView.backgroundColor = _selectLineColor;
  100. _selectView.layer.cornerRadius = 1.0;
  101. [self addSubview:_selectView];
  102. }
  103. - (void)btnClick:(UIButton *)button{
  104. if (button.tag==_selectIndex) return;
  105. UIButton *fore = (UIButton *)[self viewWithTag:_selectIndex];
  106. // fore.selected = NO;
  107. fore.titleLabel.font = _normalFont;
  108. [fore setTitleColor:_normalColor forState:UIControlStateNormal];
  109. // button.selected = YES;
  110. button.titleLabel.font = _selectFont;
  111. [button setTitleColor:_selectColor forState:UIControlStateNormal];
  112. _selectIndex = button.tag;
  113. [UIView animateWithDuration:0.25 animations:^{
  114. if (self->_isBackground) {
  115. 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);
  116. }else{
  117. 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);
  118. }
  119. }];
  120. if (self.delegate&&[self.delegate respondsToSelector:@selector(youpaifnavBtnClick:)]) {
  121. [self.delegate youpaifnavBtnClick:(button.tag-1000)];
  122. }
  123. }
  124. - (void)changeCurrentSelectButton:(NSInteger)index{
  125. NSLog(@"22%d",_isBackground);
  126. if (index+1000==_selectIndex) return;
  127. UIButton *fore = (UIButton *)[self viewWithTag:_selectIndex];
  128. // fore.selected = NO;
  129. fore.titleLabel.font = _normalFont;
  130. [fore setTitleColor:_normalColor forState:UIControlStateNormal];
  131. _selectIndex = index+1000;
  132. UIButton *behind = (UIButton *)[self viewWithTag:_selectIndex];
  133. // behind.selected = YES;
  134. behind.titleLabel.font = _selectFont;
  135. [behind setTitleColor:_selectColor forState:UIControlStateNormal];
  136. [UIView animateWithDuration:0.25 animations:^{
  137. if (self->_isBackground) {
  138. 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);
  139. }else{
  140. 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);
  141. }
  142. }];
  143. }
  144. -(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
  145. {
  146. if (self = [super initWithFrame:frame]) {
  147. NSLog(@"11111 === %ld",currentIndex);
  148. _selectIndex = currentIndex+1000;
  149. _titleArr = arr;
  150. _normalFont = font;
  151. _normalColor = normalColor;
  152. _selectFont = selectFont;
  153. _selectColor = selectcolor;
  154. _selectLineColor = selectLineColor;
  155. self.frame = frame;
  156. self.backgroundColor = [UIColor clearColor];
  157. [self youpaifsetupView2];
  158. }
  159. return self;
  160. }
  161. - (void)youpaifsetupView2{
  162. //添加按钮
  163. NSLog(@"22222 === %d",_selectIndex-1000);
  164. for (int i = 0; i<_titleArr.count; i++) {
  165. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  166. button.frame = CGRectMake(i*self.frame.size.width/_titleArr.count, 0, self.frame.size.width/_titleArr.count, self.frame.size.height);
  167. [button setTitle:_titleArr[i] forState:UIControlStateNormal];
  168. [button setTitleColor:_normalColor forState:UIControlStateNormal];
  169. button.titleLabel.font = _normalFont;
  170. if (_selectIndex-1000 == i) {
  171. button.titleLabel.font = _selectFont;
  172. [button setTitleColor:_selectColor forState:UIControlStateNormal];
  173. }
  174. button.tag = 1000+i;
  175. if (_selectIndex==button.tag) button.selected = YES;
  176. [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  177. [self addSubview:button];
  178. if (_selectIndex -1000 == 0) {
  179. [button sendActionsForControlEvents:UIControlEventTouchUpInside];
  180. }
  181. }
  182. CGFloat initX = (self.frame.size.width/_titleArr.count-20)/2;
  183. _selectView = [[UIView alloc]initWithFrame:CGRectMake(initX+(_selectIndex-1000)*self.frame.size.width/_titleArr.count, self.frame.size.height-6, 20, 2)];
  184. _selectView.backgroundColor = _selectLineColor;
  185. _selectView.layer.cornerRadius = 1.0;
  186. [self addSubview:_selectView];
  187. }
  188. @end