省钱达人

BuyLimitHeader.m 25KB

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