CustomTitleView.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // CustomTitleView.m
  3. // CGXHotBrandView-OC
  4. //
  5. // Created by CGX on 2018/05/01.
  6. // Copyright © 2020 CGX. All rights reserved.
  7. //
  8. #import "CustomTitleView.h"
  9. @interface CustomTitleView ()
  10. {
  11. UIScrollView *mainScrollViewH;
  12. }
  13. @property (nonatomic, assign,readwrite) NSInteger selectedIndex;
  14. @property (nonatomic , strong) NSMutableArray<NSString *> *titleArray;
  15. @property (nonatomic , strong) UIView *lineView;
  16. @property (nonatomic , assign) BOOL isFirst;
  17. @property (nonatomic , strong) UIButton *btn;
  18. @end
  19. @implementation CustomTitleView
  20. - (instancetype)initWithFrame:(CGRect)frame
  21. {
  22. self = [super initWithFrame:frame];
  23. if (self) {
  24. self.isFirst = YES;
  25. self.selectedIndex= 0;
  26. mainScrollViewH=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
  27. mainScrollViewH.bounces = YES;
  28. mainScrollViewH.scrollEnabled = YES;
  29. mainScrollViewH.showsHorizontalScrollIndicator = NO;
  30. mainScrollViewH.showsVerticalScrollIndicator = NO;
  31. [self addSubview:mainScrollViewH];
  32. mainScrollViewH.translatesAutoresizingMaskIntoConstraints = NO;
  33. mainScrollViewH.contentSize = CGSizeMake(frame.size.width, frame.size.height);
  34. self.lineView = [[UIView alloc] init];
  35. self.lineView.backgroundColor = [UIColor redColor];
  36. [mainScrollViewH addSubview:self.lineView];
  37. [mainScrollViewH bringSubviewToFront:self.lineView];
  38. }
  39. return self;
  40. }
  41. - (void)layoutSubviews
  42. {
  43. [super layoutSubviews];
  44. mainScrollViewH.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
  45. NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:mainScrollViewH attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
  46. NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:mainScrollViewH attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
  47. NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:mainScrollViewH attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
  48. NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:mainScrollViewH attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
  49. [self addConstraint:top];
  50. [self addConstraint:left];
  51. [self addConstraint:right];
  52. [self addConstraint:bottom];
  53. CGFloat x = 10;
  54. for (int i = 0; i<self.titleArray.count; i++) {
  55. NSString *str = self.titleArray[i];
  56. UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
  57. [mainScrollViewH addSubview:btn1];
  58. btn1.tag = 10000+i;
  59. [btn1 setTitle:str forState:UIControlStateNormal];
  60. [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  61. [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  62. btn1.titleLabel.font = [UIFont systemFontOfSize:14];
  63. CGFloat width = [str boundingRectWithSize:CGSizeMake(RQ_SCREEN_WIDTH, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.width+20;
  64. btn1.frame = CGRectMake(x, 0, width, 50);
  65. x = x + width;
  66. if (i==0) {
  67. self.lineView.frame = CGRectMake(CGRectGetMinX(btn1.frame)+(CGRectGetWidth(btn1.frame)-20)/2.0, 40, 20, 3);
  68. }
  69. if (i==0) {
  70. self.selectedIndex = i;
  71. [self btnClick:btn1];
  72. }
  73. }
  74. CGFloat scrollViewWidth = CGRectGetMaxX(mainScrollViewH.subviews.lastObject.frame);
  75. mainScrollViewH.contentSize = CGSizeMake(scrollViewWidth+10,50);
  76. }
  77. - (NSMutableArray<NSString *> *)titleArray
  78. {
  79. if (!_titleArray) {
  80. _titleArray = [NSMutableArray array];
  81. }
  82. return _titleArray;
  83. }
  84. - (void)updateDataTitieArray:(NSMutableArray<NSString *> *)titleArray
  85. {
  86. [self.titleArray removeAllObjects];
  87. [self.titleArray addObjectsFromArray:titleArray];
  88. [self setNeedsLayout];
  89. [self layoutIfNeeded];
  90. }
  91. - (void)btnClick:(UIButton *)btn
  92. {
  93. if (self.selectedIndex == btn.tag-10000 ) {
  94. if (!self.isFirst) {
  95. return;
  96. }
  97. }
  98. self.btn = btn;
  99. self.isFirst = NO;
  100. self.selectedIndex = btn.tag-10000;
  101. __weak typeof(self) weakSelf=self;
  102. [UIView animateWithDuration:0.5 animations:^{
  103. weakSelf.lineView.frame = CGRectMake(CGRectGetMinX(btn.frame)+(CGRectGetWidth(btn.frame)-20)/2.0, weakSelf.lineView.frame.origin.y, weakSelf.lineView.frame.size.width, weakSelf.lineView.frame.size.height);
  104. }];
  105. // 计算偏移量
  106. CGFloat offsetX = btn.center.x - CGRectGetWidth(mainScrollViewH.frame) * 0.5;
  107. if (offsetX < 0) offsetX = 0;
  108. // 获取最大滚动范围
  109. CGFloat maxOffsetX = mainScrollViewH.contentSize.width - CGRectGetWidth(mainScrollViewH.frame);
  110. if (offsetX > maxOffsetX) offsetX = maxOffsetX;
  111. // 滚动标题滚动条
  112. [mainScrollViewH setContentOffset:CGPointMake(offsetX, 0) animated:YES];
  113. if (self.titieSelectBlock) {
  114. self.titieSelectBlock(btn.tag-10000);
  115. }
  116. [self selectItemAtIndex:self.selectedIndex];
  117. }
  118. - (void)selectItemAtIndex:(NSInteger)index
  119. {
  120. self.isFirst = NO;
  121. self.selectedIndex = index;
  122. __weak typeof(self) weakSelf=self;
  123. [UIView animateWithDuration:0.5 animations:^{
  124. weakSelf.lineView.frame = CGRectMake(CGRectGetMinX(self.btn.frame)+(CGRectGetWidth(self.btn.frame)-20)/2.0, weakSelf.lineView.frame.origin.y, weakSelf.lineView.frame.size.width, weakSelf.lineView.frame.size.height);
  125. }];
  126. // 计算偏移量
  127. CGFloat offsetX = self.btn.center.x - CGRectGetWidth(mainScrollViewH.frame) * 0.5;
  128. if (offsetX < 0) offsetX = 0;
  129. // 获取最大滚动范围
  130. CGFloat maxOffsetX = mainScrollViewH.contentSize.width - CGRectGetWidth(mainScrollViewH.frame);
  131. if (offsetX > maxOffsetX) offsetX = maxOffsetX;
  132. // 滚动标题滚动条
  133. [mainScrollViewH setContentOffset:CGPointMake(offsetX, 0) animated:YES];
  134. if (self.titieSelectBlock) {
  135. self.titieSelectBlock(self.btn.tag-10000);
  136. }
  137. }
  138. /*
  139. // Only override drawRect: if you perform custom drawing.
  140. // An empty implementation adversely affects performance during animation.
  141. - (void)drawRect:(CGRect)rect {
  142. // Drawing code
  143. }
  144. */
  145. @end