省钱达人

MLMSegmentHead.m 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. //
  2. // MLMSegmentHead.m
  3. // MLMSegmentPage
  4. //
  5. // Created by my on 16/11/4.
  6. // Copyright © 2016年 my. All rights reserved.
  7. //
  8. #import "MLMSegmentHead.h"
  9. #import "SegmentPageHead.h"
  10. #define SCROLL_WIDTH (self.width - _moreButton_width)
  11. #define SCROLL_HEIGHT (self.height - _bottomLineHeight)
  12. #define CURRENT_WIDTH(s) [titleWidthArray[s] floatValue]
  13. static CGFloat arrow_H = 6;//箭头高
  14. static CGFloat arrow_W = 18;//箭头宽
  15. static CGFloat animation_time = .3;
  16. @interface MLMSegmentHead ()
  17. /*------------其他设置------------*/
  18. /**
  19. * MLMSegmentHeadStyle
  20. */
  21. @property (nonatomic, assign) MLMSegmentHeadStyle headStyle;
  22. /**
  23. * MLMSegmentHeadStyle
  24. */
  25. @property (nonatomic, assign) MLMSegmentLayoutStyle layoutStyle;
  26. @end
  27. @implementation MLMSegmentHead
  28. {
  29. NSMutableArray *titlesArray;///标题数组
  30. UIScrollView *titlesScroll;
  31. NSMutableArray *buttonArray;//按钮数组
  32. NSMutableArray *backImgArray;//背景图数组
  33. UIView *lineView;//下划线view
  34. CAShapeLayer *arrow_layer;//箭头layer
  35. UIView *slideView;//滑块View
  36. UIScrollView *slideScroll;
  37. UIView *bottomLineView;//分割线
  38. NSInteger currentIndex;//当前选中的按钮
  39. //在与外侧scroll关联时,动画结束之后将其设置为NO
  40. BOOL isSelected;//区分点击还是滑动
  41. //button宽度的数组,总宽度
  42. NSMutableArray *titleWidthArray;
  43. CGFloat sum_width;
  44. //用来判断向左向右
  45. CGFloat endScale;
  46. }
  47. #pragma mark - initMethod
  48. - (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles {
  49. return [self initWithFrame:frame titles:titles headStyle:SegmentHeadStyleDefault];
  50. }
  51. - (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles headStyle:(MLMSegmentHeadStyle)style {
  52. return [self initWithFrame:frame titles:titles headStyle:style layoutStyle:MLMSegmentLayoutDefault];
  53. }
  54. - (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles headStyle:(MLMSegmentHeadStyle)style layoutStyle:(MLMSegmentLayoutStyle)layout {
  55. if (self = [super initWithFrame:frame]) {
  56. _headStyle = style;
  57. _layoutStyle = layout;
  58. titlesArray = [titles mutableCopy];
  59. [self initCustom];
  60. }
  61. return self;
  62. }
  63. #pragma mark - custom init
  64. - (void)initCustom {
  65. _headColor = [UIColor whiteColor];
  66. _selectColor = [UIColor blackColor];
  67. _deSelectColor = [UIColor lightGrayColor];
  68. _moreButton_width = 0;
  69. buttonArray = [NSMutableArray array];
  70. backImgArray = [NSMutableArray array];
  71. _showIndex = 0;
  72. _fontSize = 13;
  73. _fontScale = 1;
  74. _singleW_Add = 20;
  75. _lineColor = _selectColor;
  76. _lineHeight = 2.5;
  77. _lineScale = 1;
  78. _arrowColor = _selectColor;
  79. _slideHeight = SCROLL_HEIGHT;
  80. _slideColor = _deSelectColor;
  81. _slideCorner = _slideHeight/2;
  82. _slideScale = 1;
  83. _maxTitles = 5.0;
  84. _bottomLineColor = [UIColor grayColor];
  85. _bottomLineHeight = 1;
  86. }
  87. - (void)changeTitle:(NSArray *)titles {
  88. titlesArray = [titles mutableCopy];
  89. [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  90. [self defaultAndCreateView];
  91. }
  92. #pragma mark - layout
  93. - (void)defaultAndCreateView {
  94. if (!titleWidthArray) {
  95. titleWidthArray = [NSMutableArray arrayWithCapacity:titlesArray.count];
  96. }
  97. [titleWidthArray removeAllObjects];
  98. _maxTitles = _maxTitles>titlesArray.count?titlesArray.count:_maxTitles;
  99. [self titlesWidth];
  100. if (_equalSize) {
  101. self.width = sum_width+_moreButton_width;
  102. if (titlesScroll) {
  103. titlesScroll.width = SCREEN_WIDTH;
  104. }
  105. if (slideScroll) {
  106. slideScroll.width = SCREEN_WIDTH;
  107. }
  108. }
  109. //判断总宽度
  110. if (sum_width > SCROLL_WIDTH && _layoutStyle== MLMSegmentLayoutCenter) {
  111. _layoutStyle = MLMSegmentLayoutLeft;
  112. }
  113. _showIndex = MIN(titlesArray.count-1, MAX(0, _showIndex));
  114. [self createView];
  115. if (_showIndex != 0) {
  116. currentIndex = _showIndex;
  117. [self changeContentOffset];
  118. [self changeBtnFrom:0 to:currentIndex];
  119. }
  120. }
  121. #pragma mark - 根据文字计算宽度
  122. - (void)titlesWidth {
  123. sum_width = 0;
  124. CGFloat width = SCROLL_WIDTH/_maxTitles;
  125. for (NSString *title in titlesArray) {
  126. if (_layoutStyle != MLMSegmentLayoutDefault) {
  127. width = [self titleWidth:title];
  128. }
  129. [titleWidthArray addObject:@(width)];
  130. sum_width += width;
  131. }
  132. }
  133. - (CGFloat)titleWidth:(NSString *)title {
  134. CGFloat sys_font = _fontScale>1?_fontSize*_fontScale:_fontSize;
  135. return [title boundingRectWithSize:CGSizeMake(MAXFLOAT, CGRectGetHeight(self.frame)) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:sys_font]} context:nil].size.width + _singleW_Add;
  136. }
  137. //#pragma mark - 添加按钮
  138. //- (void)addMoreTitles:(NSArray *)moreTitles {
  139. // if (_layoutStyle == MLMSegmentLayoutCenter) {
  140. // return;
  141. // }
  142. //
  143. // CGFloat start_x = sum_width;
  144. // CGFloat start_index = titleWidthArray.count;
  145. //
  146. // //添加到数组,并计算宽度
  147. // for (NSInteger i = 0; i < moreTitles.count; i ++) {
  148. // NSString *title = moreTitles[i];
  149. // CGFloat width = [self titleWidth:title];
  150. // [titleWidthArray addObject:@(width)];
  151. // sum_width += width;
  152. //
  153. // [titlesArray addObject:title];
  154. // }
  155. //
  156. // [self createBtn:titlesArray addScroll:titlesScroll startX:start_x start_index:start_index];
  157. // if (_headStyle == SegmentHeadStyleSlide) {
  158. // [self createBtn:titlesArray addScroll:slideScroll startX:start_x start_index:start_index];
  159. // }
  160. //
  161. //
  162. // [self setSelectIndex:currentIndex];
  163. //}
  164. #pragma mark - create View
  165. - (void)createView {
  166. _fontScale = _headStyle==SegmentHeadStyleSlide?1:_fontScale;
  167. titlesScroll = [self customScroll];
  168. [self scrollViewSubviews:titlesScroll];
  169. [self addSubview:titlesScroll];
  170. if (_moreButton_width != 0) {
  171. _moreButton = [[UIView alloc] init];
  172. _moreButton.frame = CGRectMake(CGRectGetMaxX(titlesScroll.frame), 0, _moreButton_width, titlesScroll.height);
  173. [self addSubview:_moreButton];
  174. }
  175. if (_bottomLineHeight) {
  176. bottomLineView = [self bottomLineView];
  177. [self addSubview:bottomLineView];
  178. }
  179. switch (_headStyle) {
  180. case SegmentHeadStyleLine:
  181. {
  182. lineView = [self lineView];
  183. [titlesScroll addSubview:lineView];
  184. }
  185. break;
  186. case SegmentHeadStyleArrow:
  187. {
  188. _lineHeight = arrow_H;
  189. _lineScale = 1;
  190. lineView = [self lineView];
  191. lineView.backgroundColor = [UIColor clearColor];
  192. [titlesScroll addSubview:lineView];
  193. //arrow
  194. [self drawArrowLayer];
  195. arrow_layer.position = CGPointMake(lineView.width/2, lineView.height/2);
  196. [lineView.layer addSublayer:arrow_layer];
  197. }
  198. break;
  199. case SegmentHeadStyleSlide:
  200. {
  201. slideView = [self slideView];
  202. [titlesScroll addSubview:slideView];
  203. }
  204. break;
  205. default:
  206. break;
  207. }
  208. }
  209. #pragma mark - drow arrow
  210. - (void)drawArrowLayer {
  211. arrow_layer = [[CAShapeLayer alloc] init];
  212. arrow_layer.bounds = CGRectMake(0, 0, arrow_W, arrow_H);
  213. [arrow_layer setFillColor:_arrowColor.CGColor];
  214. UIBezierPath *arrowPath = [UIBezierPath bezierPath];
  215. [arrowPath moveToPoint:CGPointMake(arrow_W/2, 0)];
  216. [arrowPath addLineToPoint:CGPointMake(arrow_W, arrow_H)];
  217. [arrowPath addLineToPoint:CGPointMake(0, arrow_H)];
  218. [arrowPath closePath];
  219. arrow_layer.path = arrowPath.CGPath;
  220. }
  221. #pragma mark - create customScroll
  222. - (UIScrollView *)customScroll {
  223. if (!titlesArray) {
  224. return nil;
  225. }
  226. UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCROLL_WIDTH, SCROLL_HEIGHT)];
  227. scroll.contentSize = CGSizeMake(MAX(SCROLL_WIDTH, sum_width), SCROLL_HEIGHT);
  228. scroll.backgroundColor = _headColor;
  229. scroll.showsVerticalScrollIndicator = NO;
  230. scroll.showsHorizontalScrollIndicator = NO;
  231. scroll.bounces = NO;
  232. return scroll;
  233. }
  234. #pragma mark - titlesScroll subviews - yes or slideScroll subviews - no
  235. - (void)scrollViewSubviews:(UIScrollView*)scroll {
  236. BOOL titles = [scroll isEqual:titlesScroll];
  237. CGFloat start_x = 0;
  238. if (_layoutStyle == MLMSegmentLayoutCenter) {
  239. //计算布局的起点
  240. start_x = SCROLL_WIDTH/2;
  241. for (NSInteger i = 0; i < titleWidthArray.count/2; i ++) {
  242. start_x -= CURRENT_WIDTH(i);
  243. }
  244. if (titlesArray.count%2 != 0) {
  245. start_x -= CURRENT_WIDTH(titleWidthArray.count/2)/2;
  246. }
  247. }
  248. [self createBtn:titlesArray addScroll:scroll startX:start_x start_index:0];
  249. if (titles && _headStyle != SegmentHeadStyleSlide) {
  250. UIButton *curBtn = buttonArray[_showIndex];
  251. if (_fontScale != 1) {
  252. curBtn.titleLabel.font = [UIFont systemFontOfSize:_fontSize*_fontScale];
  253. }
  254. [curBtn setTintColor:_selectColor];
  255. }
  256. }
  257. #pragma mark - createBtn
  258. - (void)createBtn:(NSArray *)titlesArr addScroll:(UIScrollView*)scroll startX:(CGFloat)start_x start_index:(NSInteger)start_index {
  259. BOOL titles = [scroll isEqual:titlesScroll];
  260. CGFloat width;
  261. for (NSInteger i = start_index; i < titlesArr.count; i ++) {
  262. width = CURRENT_WIDTH(i);
  263. UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
  264. [button setTitle:titlesArr[i] forState:UIControlStateNormal];
  265. button.titleLabel.font = [UIFont systemFontOfSize:_fontSize];
  266. button.frame = CGRectMake(start_x, 0, width, SCROLL_HEIGHT);
  267. start_x += width;
  268. if (titles) {
  269. [button setTintColor:_deSelectColor];
  270. [button addTarget:self action:@selector(selectedHeadTitles:) forControlEvents:UIControlEventTouchUpInside];
  271. [buttonArray addObject:button];
  272. //
  273. UIImageView *imgV = [[UIImageView alloc] initWithFrame:button.frame];
  274. [scroll addSubview:imgV];
  275. [backImgArray addObject:imgV];
  276. } else {
  277. [button setTintColor:_selectColor];
  278. }
  279. [scroll addSubview:button];
  280. }
  281. scroll.contentSize = CGSizeMake(MAX(SCROLL_WIDTH, sum_width), SCROLL_HEIGHT);
  282. }
  283. - (void)setBackImages:(NSArray *)backImages {
  284. _backImages = backImages;
  285. NSInteger count = MIN(backImages.count, backImgArray.count);
  286. for (NSInteger i = 0; i < count; i ++) {
  287. UIImageView *imageV = backImgArray[i];
  288. [imageV setImage:backImages[i]];
  289. if (i == currentIndex) {
  290. imageV.alpha = 1;
  291. } else {
  292. imageV.alpha = 0;
  293. }
  294. }
  295. }
  296. #pragma mark - create Line
  297. - (UIView *)lineView {
  298. _lineScale = fabs(_lineScale)>1?1:fabs(_lineScale);
  299. CGFloat line_w = CURRENT_WIDTH(currentIndex);
  300. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, SCROLL_HEIGHT-_lineHeight, line_w*_lineScale, _lineHeight)];
  301. UIButton *current_btn = buttonArray[currentIndex];
  302. line.center = CGPointMake(current_btn.center.x, line.center.y);
  303. line.backgroundColor = _lineColor;
  304. return line;
  305. }
  306. #pragma mark - bottom Line
  307. - (UIView *)bottomLineView {
  308. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, SCROLL_HEIGHT, self.width, _bottomLineHeight)];
  309. line.backgroundColor = _bottomLineColor;
  310. return line;
  311. }
  312. #pragma mark - create slide
  313. - (UIView *)slideView {
  314. CGFloat slide_w = CURRENT_WIDTH(currentIndex);
  315. UIView *slide = [[UIView alloc] initWithFrame:CGRectMake(0, (SCROLL_HEIGHT-_slideHeight)/2, slide_w*_slideScale, _slideHeight)];
  316. UIButton *current_btn = buttonArray[currentIndex];
  317. slide.center = CGPointMake(current_btn.center.x, slide.center.y);
  318. slide.clipsToBounds = YES;
  319. slide.layer.cornerRadius = MIN(_slideCorner, _slideHeight/2);
  320. slide.backgroundColor = _slideColor;
  321. slideScroll = [self customScroll];
  322. [self scrollViewSubviews:slideScroll];
  323. slideScroll.userInteractionEnabled = NO;
  324. slideScroll.backgroundColor = [UIColor clearColor];
  325. CGRect convertRect = [slide convertRect:titlesScroll.frame fromView:titlesScroll.superview];
  326. slideScroll.frame = CGRectMake(convertRect.origin.x, -(SCROLL_HEIGHT - _slideHeight)/2, SCROLL_WIDTH, SCROLL_HEIGHT);
  327. [slide addSubview:slideScroll];
  328. return slide;
  329. }
  330. #pragma mark - button Action
  331. - (void)selectedHeadTitles:(UIButton *)button {
  332. NSInteger selectIndex = [buttonArray indexOfObject:button];
  333. [self changeIndex:selectIndex completion:YES];
  334. }
  335. #pragma mark - 点击结束
  336. - (void)animationEnd {
  337. isSelected = NO;
  338. }
  339. #pragma mark - set index
  340. - (void)setSelectIndex:(NSInteger)index {
  341. // if (index == currentIndex) {
  342. // return;
  343. // }
  344. // //before
  345. // NSInteger before = currentIndex;
  346. // currentIndex = index;
  347. // [self changeContentOffset];
  348. // //select
  349. // [UIView animateWithDuration:animation_time animations:^{
  350. // [self changeBtnFrom:before to:currentIndex];
  351. // } completion:^(BOOL finished) {
  352. // }];
  353. // isSelected = YES;
  354. // if ([self.delegate respondsToSelector:@selector(didSelectedIndex:)]) {
  355. // [self.delegate didSelectedIndex:currentIndex];
  356. // } else if (self.selectedIndex) {
  357. // self.selectedIndex(currentIndex);
  358. // }
  359. [self changeIndex:index completion:NO];
  360. }
  361. - (void)changeIndex:(NSInteger)index completion:(BOOL)completion {
  362. if (index == currentIndex) {
  363. return;
  364. }
  365. //before
  366. NSInteger before = currentIndex;
  367. currentIndex = index;
  368. [self changeContentOffset];
  369. //select
  370. [UIView animateWithDuration:animation_time animations:^{
  371. [self changeBtnFrom:before to:currentIndex];
  372. } completion:^(BOOL finished) {
  373. }];
  374. isSelected = YES;
  375. if (completion) {
  376. if ([self.delegate respondsToSelector:@selector(didSelectedIndex:)]) {
  377. [self.delegate didSelectedIndex:currentIndex];
  378. } else if (self.selectedIndex) {
  379. self.selectedIndex(currentIndex);
  380. }
  381. }
  382. }
  383. - (void)changeIndex:(NSInteger)index completion:(BOOL)completion animate:(BOOL)animate {
  384. if (index == currentIndex) {
  385. return;
  386. }
  387. //before
  388. NSInteger before = currentIndex;
  389. currentIndex = index;
  390. [self changeContentOffset];
  391. //select
  392. if (animate) {
  393. [UIView animateWithDuration:animation_time animations:^{
  394. [self changeBtnFrom:before to:currentIndex];
  395. } completion:^(BOOL finished) {
  396. }];
  397. }else{
  398. [self changeBtnFrom:before to:currentIndex];
  399. }
  400. isSelected = YES;
  401. if (completion) {
  402. if ([self.delegate respondsToSelector:@selector(didSelectedIndex:)]) {
  403. [self.delegate didSelectedIndex:currentIndex];
  404. } else if (self.selectedIndex) {
  405. self.selectedIndex(currentIndex);
  406. }
  407. }
  408. }
  409. - (void)changeContentOffset {
  410. if (sum_width > SCROLL_WIDTH) {
  411. UIButton *currentBtn = buttonArray[currentIndex];
  412. if (currentBtn.center.x<SCROLL_WIDTH/2) {
  413. [titlesScroll setContentOffset:CGPointMake(0, 0) animated:YES];
  414. } else if (currentBtn.center.x > (sum_width-SCROLL_WIDTH/2)) {
  415. [titlesScroll setContentOffset:CGPointMake(sum_width-SCROLL_WIDTH, 0) animated:YES];
  416. } else {
  417. [titlesScroll setContentOffset:CGPointMake(currentBtn.center.x - SCROLL_WIDTH/2, 0) animated:YES];
  418. }
  419. }
  420. }
  421. - (void)changeBtnFrom:(NSInteger)from to:(NSInteger)to {
  422. UIButton *before_btn = buttonArray[from];
  423. UIButton *select_btn = buttonArray[to];
  424. if (_headStyle != SegmentHeadStyleSlide) {
  425. [before_btn setTintColor:_deSelectColor];
  426. [select_btn setTintColor:_selectColor];
  427. }
  428. if (_fontScale) {
  429. before_btn.titleLabel.font = [UIFont systemFontOfSize:_fontSize];
  430. select_btn.titleLabel.font = [UIFont systemFontOfSize:_fontSize*_fontScale];
  431. }
  432. if (lineView) {
  433. lineView.width = select_btn.width*_lineScale;
  434. lineView.center = CGPointMake(select_btn.center.x, lineView.center.y);
  435. }
  436. if (arrow_layer) {
  437. arrow_layer.position = CGPointMake(lineView.width/2, lineView.height/2);
  438. }
  439. if (slideView) {
  440. //slide位置变化
  441. slideView.width = select_btn.width*_slideScale;
  442. slideView.center = CGPointMake(select_btn.center.x, slideView.center.y);
  443. //偏移
  444. CGRect convertRect = [slideView convertRect:titlesScroll.frame fromView:titlesScroll];
  445. slideScroll.frame = CGRectMake(convertRect.origin.x, convertRect.origin.y, slideScroll.contentSize.width, slideScroll.contentSize.height);
  446. }
  447. if (_hadBackImg) {
  448. UIImageView *before_img = backImgArray[from];
  449. UIImageView *select_img = backImgArray[to];
  450. before_img.alpha = 0;
  451. select_img.alpha = 1;
  452. }
  453. }
  454. #pragma mark - animation
  455. //外部关联的scrollView变化
  456. - (void)changePointScale:(CGFloat)scale {
  457. if (isSelected) {
  458. return;
  459. }
  460. if (scale<0) {
  461. return;
  462. }
  463. //区分向左 还是向右
  464. BOOL left = endScale > scale;
  465. endScale = scale;
  466. //1.将scale变为对应titleScroll的titleScale
  467. //每个view所占的百分比
  468. CGFloat per_view = 1.0/(CGFloat)titlesArray.count;
  469. //下标
  470. NSInteger changeIndex = scale/per_view + (left?1:0);
  471. NSInteger nextIndex = changeIndex + (left?-1:1);
  472. //超出范围
  473. if (nextIndex >= titlesArray.count || changeIndex >= titlesArray.count) {
  474. return;
  475. }
  476. //currentbtn
  477. UIButton *currentBtn = buttonArray[changeIndex];
  478. UIButton *nextBtn = buttonArray[nextIndex];
  479. //startscla
  480. CGFloat start_scale = 0;
  481. for (NSInteger i = 0; i < nextIndex; i++) {
  482. start_scale += CURRENT_WIDTH(i)/sum_width;
  483. }
  484. //滑动选中位置所占的相对百分比
  485. CGFloat current_title_Scale = CURRENT_WIDTH(changeIndex)/sum_width;
  486. //单个view偏移的百分比
  487. CGFloat single_offset_scale = (scale - per_view*changeIndex)/per_view;
  488. //转换成对应title的百分比
  489. CGFloat titleScale = single_offset_scale * current_title_Scale + start_scale;
  490. //变化的百分比
  491. CGFloat change_scale = (left?-1:1)*(titleScale - start_scale)/current_title_Scale;
  492. switch (_headStyle) {
  493. case SegmentHeadStyleDefault:
  494. case SegmentHeadStyleLine:
  495. case SegmentHeadStyleArrow:
  496. {
  497. if (lineView) {
  498. //lineView位置变化
  499. lineView.width = [self widthChangeCurWidth:CURRENT_WIDTH(changeIndex) nextWidth:CURRENT_WIDTH(nextIndex) changeScale:change_scale endScale:_lineScale];
  500. CGFloat center_x = [self centerChanegCurBtn:currentBtn nextBtn:nextBtn changeScale:change_scale];
  501. lineView.center = CGPointMake(center_x, lineView.center.y);
  502. }
  503. if (arrow_layer) {
  504. arrow_layer.position = CGPointMake(lineView.width/2, lineView.height/2);
  505. }
  506. //颜色变化
  507. [self colorChangeCurBtn:currentBtn nextBtn:nextBtn changeScale:change_scale];
  508. //字体大小变化
  509. [self fontChangeCurBtn:currentBtn nextBtn:nextBtn changeScale:change_scale];
  510. //背景图片
  511. if (_hadBackImg) {
  512. UIImageView *current_img = backImgArray[changeIndex];
  513. UIImageView *next_img = backImgArray[nextIndex];
  514. [self backImgCurImg:current_img nextImg:next_img changeScale:change_scale];
  515. }
  516. }
  517. break;
  518. case SegmentHeadStyleSlide:
  519. {
  520. //slide位置变化
  521. slideView.width = [self widthChangeCurWidth:CURRENT_WIDTH(changeIndex) nextWidth:CURRENT_WIDTH(nextIndex) changeScale:change_scale endScale:_slideScale];
  522. CGFloat center_x = [self centerChanegCurBtn:currentBtn nextBtn:nextBtn changeScale:change_scale];
  523. slideView.center = CGPointMake(center_x, slideView.center.y);
  524. //偏移
  525. CGRect convertRect = [slideView convertRect:titlesScroll.frame fromView:titlesScroll];
  526. slideScroll.frame = CGRectMake(convertRect.origin.x, convertRect.origin.y, slideScroll.contentSize.width, slideScroll.contentSize.height);
  527. }
  528. break;
  529. default:
  530. break;
  531. }
  532. }
  533. #pragma mark - 长度变化
  534. - (CGFloat)widthChangeCurWidth:(CGFloat)curWidth nextWidth:(CGFloat)nextWidth changeScale:(CGFloat)changeScale endScale:(CGFloat)endscale{
  535. //改变的宽度
  536. CGFloat change_width = curWidth - nextWidth;
  537. //宽度变化
  538. CGFloat width = curWidth*endscale - changeScale * change_width;
  539. return width;
  540. }
  541. #pragma mark - 中心位置的变化
  542. - (CGFloat)centerChanegCurBtn:(UIButton *)curBtn nextBtn:(UIButton *)nextBtn changeScale:(CGFloat)changeScale {
  543. //lineView改变的中心
  544. CGFloat change_center = nextBtn.center.x - curBtn.center.x;
  545. //lineView位置变化
  546. CGFloat center_x = curBtn.center.x + changeScale * change_center;
  547. return center_x;
  548. }
  549. #pragma mark - 字体大小变化
  550. - (void)fontChangeCurBtn:(UIButton *)curBtn nextBtn:(UIButton *)nextBtn changeScale:(CGFloat)changeScale{
  551. //button字体改变的大小
  552. CGFloat btn_font_change = _fontSize*(_fontScale - 1);
  553. //大小变化
  554. CGFloat next_font = _fontSize + changeScale*btn_font_change;
  555. CGFloat cur_font = _fontSize*_fontScale - changeScale*btn_font_change;
  556. nextBtn.titleLabel.font = [UIFont systemFontOfSize:next_font];
  557. curBtn.titleLabel.font = [UIFont systemFontOfSize:cur_font];
  558. }
  559. #pragma mark - 颜色变化
  560. - (void)colorChangeCurBtn:(UIButton *)curBtn nextBtn:(UIButton *)nextBtn changeScale:(CGFloat)changeScale {
  561. //button选中颜色
  562. CGFloat sel_red;
  563. CGFloat sel_green;
  564. CGFloat sel_blue;
  565. CGFloat sel_alpha;
  566. //button未选中的颜色
  567. CGFloat de_sel_red;
  568. CGFloat de_sel_green;
  569. CGFloat de_sel_blue;
  570. CGFloat de_sel_alpha;
  571. if ([_selectColor getRed:&sel_red green:&sel_green blue:&sel_blue alpha:&sel_alpha] && [_deSelectColor getRed:&de_sel_red green:&de_sel_green blue:&de_sel_blue alpha:&de_sel_alpha]) {
  572. //颜色的变化的大小
  573. CGFloat red_changge = sel_red - de_sel_red;
  574. CGFloat green_changge = sel_green - de_sel_green;
  575. CGFloat blue_changge = sel_blue - de_sel_blue;
  576. CGFloat alpha_changge = sel_alpha - de_sel_alpha;
  577. //颜色变化
  578. [nextBtn setTintColor:[UIColor colorWithRed:de_sel_red + red_changge*changeScale
  579. green:de_sel_green + green_changge*changeScale
  580. blue:de_sel_blue + blue_changge*changeScale
  581. alpha:de_sel_alpha + alpha_changge*changeScale]];
  582. [curBtn setTintColor:[UIColor colorWithRed:sel_red - red_changge*changeScale
  583. green:sel_green - green_changge*changeScale
  584. blue:sel_blue - blue_changge*changeScale
  585. alpha:sel_alpha - alpha_changge*changeScale]];
  586. }
  587. }
  588. #pragma mark - 背景图渐变
  589. - (void)backImgCurImg:(UIImageView *)curback nextImg:(UIImageView *)nextback changeScale:(CGFloat)changeScale {
  590. //alpha变化
  591. CGFloat next_alpha = changeScale;
  592. CGFloat cur_alpha = 1 - changeScale;
  593. nextback.alpha = next_alpha>.8?1.:next_alpha;
  594. curback.alpha = cur_alpha<.2?0:cur_alpha;
  595. }
  596. #pragma mark - get sumWidth
  597. - (CGFloat)getSumWidth {
  598. return sum_width;
  599. }
  600. #pragma mark - lineView
  601. - (UIView *)getLineView {
  602. return lineView;
  603. }
  604. - (UIView *)getBottomLineView {
  605. return bottomLineView;
  606. }
  607. - (UIView *)getScrollLineView {
  608. if (_headStyle == SegmentHeadStyleLine) {
  609. return lineView;
  610. } else {
  611. return nil;
  612. }
  613. }
  614. #pragma mark - dealloc
  615. - (void)dealloc {
  616. arrow_layer.delegate = nil;
  617. [arrow_layer removeFromSuperlayer];
  618. arrow_layer = nil;
  619. }
  620. - (UIScrollView *)titlesScroll {
  621. return titlesScroll;
  622. }
  623. - (NSArray *)buttons {
  624. return buttonArray;
  625. }
  626. @end