ScrollMenuView.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #import "ScrollMenuView.h"
  2. #import "YXMenuItemView.h"
  3. @interface ScrollMenuView()<UIScrollViewDelegate>
  4. {
  5. CGFloat _itemSpace;
  6. }
  7. @property(nonatomic,strong) UIScrollView *menuScrollView;
  8. @property(nonatomic,strong) UIScrollView *contentView;
  9. @property(nonatomic,strong) NSMutableArray *menuBtnArray;
  10. @property (nonatomic, assign) NSInteger selectedIndex;
  11. @end
  12. @implementation ScrollMenuView
  13. -(instancetype)initWithFrame:(CGRect)frame
  14. {
  15. self = [super initWithFrame:frame];
  16. if (self) {
  17. _itemSpace = 20;
  18. self.selectedIndex = 0;
  19. self.menuBtnArray = [[NSMutableArray alloc]init];
  20. UIScrollView *menuScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 40)];
  21. menuScrollView.delegate = self;
  22. menuScrollView.showsVerticalScrollIndicator = NO;
  23. menuScrollView.showsHorizontalScrollIndicator = NO;
  24. [self addSubview:menuScrollView];
  25. self.menuScrollView = menuScrollView;
  26. UIScrollView *contentView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(menuScrollView.frame), frame.size.width, frame.size.height - CGRectGetHeight(menuScrollView.frame))];
  27. contentView.delegate = self;
  28. contentView.showsVerticalScrollIndicator = NO;
  29. contentView.showsHorizontalScrollIndicator = NO;
  30. contentView.pagingEnabled = YES;
  31. [self addSubview:contentView];
  32. self.contentView = contentView;
  33. }
  34. return self;
  35. }
  36. -(void)setScrollMenuView {
  37. [self.menuBtnArray removeAllObjects];
  38. [self.menuScrollView removeAllSubviews];
  39. [self.contentView removeAllSubviews];
  40. if (self.titleArray.count > 0) {
  41. CGFloat allWidth = 0;
  42. for (int i = 0; i < self.titleArray.count; i++) {
  43. NSString *text = _titleArray[i];
  44. YXMenuItemView *menuItemView = [[YXMenuItemView alloc]init];
  45. NSDictionary *dict = @{NSFontAttributeName: menuItemView.textLabel.font};
  46. CGSize textSize = [text boundingRectWithSize:CGSizeMake(200, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
  47. CGFloat width = textSize.width+2;
  48. [menuItemView.colorView mas_updateConstraints:^(MASConstraintMaker *make) {
  49. make.width.equalTo(@(width));
  50. }];
  51. menuItemView.frame = CGRectMake(i*_itemSpace + allWidth + 10, 0, width, 30);
  52. allWidth = allWidth + width;
  53. menuItemView.textLabel.text = text;
  54. if (i == self.selectedIndex) {
  55. menuItemView.colorView.hidden = NO;
  56. menuItemView.textLabel.textColor = [UIColor colorWithHexString:@"#333333"];
  57. }else{
  58. menuItemView.colorView.hidden = YES;
  59. menuItemView.textLabel.textColor = [UIColor colorWithHexString:@"#999999"];
  60. }
  61. [self.menuScrollView addSubview:menuItemView];
  62. menuItemView.tag = i;
  63. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(menuItemTapAction:)];
  64. [menuItemView addGestureRecognizer:tap];
  65. }
  66. self.menuScrollView.contentSize = CGSizeMake(_titleArray.count * _itemSpace + allWidth, 30);
  67. }else{
  68. NSLog(@"--- titleArray为空 ---");
  69. }
  70. if (self.subArray.count > 0) {
  71. self.contentView.contentSize = CGSizeMake(self.subArray.count * CGRectGetWidth(self.contentView.frame), CGRectGetHeight(self.contentView.frame));
  72. for (int j = 0; j < self.subArray.count; j++) {
  73. UIView *tempView = self.subArray[j];
  74. tempView.frame = CGRectMake(j * CGRectGetWidth(self.contentView.frame), 0, CGRectGetWidth(self.contentView.frame), CGRectGetHeight(self.contentView.frame));
  75. [self.contentView addSubview:tempView];
  76. }
  77. }else{
  78. NSLog(@"--- subArray为空 ---");
  79. }
  80. }
  81. -(void)menuItemTapAction:(UITapGestureRecognizer *)tap{
  82. NSInteger tag = tap.view.tag;
  83. self.selectedIndex = tag;
  84. [self changeViewWithAnimate:self.selectedIndex];
  85. }
  86. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  87. {
  88. if ([scrollView isEqual:self.contentView]) {
  89. NSLog(@"--------- %f",scrollView.contentOffset.x);
  90. int num = scrollView.contentOffset.x * pow(10, 6);
  91. int tempNum = SCREEN_WIDTH * pow(10, 6);
  92. if (num % tempNum != 0) {
  93. return;
  94. }
  95. int index = scrollView.contentOffset.x/SCREEN_WIDTH;
  96. NSLog(@"-----index---- %d",index);
  97. if ([self.delegate respondsToSelector:@selector(scrollMenuViewAtIndexPath:)]) {
  98. [self.delegate scrollMenuViewAtIndexPath:index];
  99. }
  100. self.selectedIndex = index;
  101. [self changeViewWithAnimate:self.selectedIndex];
  102. }
  103. }
  104. -(void)changeViewWithAnimate:(NSInteger)index {
  105. NSArray *subViews = self.menuScrollView.subviews;
  106. if (self.menuScrollView.subviews.count > index) {
  107. UIView *view = self.menuScrollView.subviews[index];
  108. [self.menuScrollView scrollRectToVisible:view.frame animated:YES];
  109. }
  110. for (int i = 0; i < self.menuScrollView.subviews.count; i++) {
  111. YXMenuItemView *menuItemView = self.menuScrollView.subviews[i];
  112. if (i == index) {
  113. menuItemView.colorView.hidden = NO;
  114. menuItemView.textLabel.textColor = [UIColor colorWithHexString:@"#333333"];
  115. }else{
  116. menuItemView.colorView.hidden = YES;
  117. menuItemView.textLabel.textColor = [UIColor colorWithHexString:@"#999999"];
  118. }
  119. }
  120. self.contentView.contentOffset = CGPointMake(index * CGRectGetWidth(self.contentView.frame), 0);
  121. if ([self.delegate respondsToSelector:@selector(scrollMenuViewAtIndexPath:)]) {
  122. [self.delegate scrollMenuViewAtIndexPath:index];
  123. }
  124. }
  125. -(UIColor *)backColor
  126. {
  127. if (_backColor == nil) {
  128. self.menuScrollView.backgroundColor = [UIColor grayColor];
  129. return [UIColor grayColor];
  130. }
  131. self.menuScrollView.backgroundColor = _backColor;
  132. return _backColor;
  133. }
  134. -(UIColor *)normalColor
  135. {
  136. if (_normalColor == nil) {
  137. return [UIColor blackColor];
  138. }
  139. return _normalColor;
  140. }
  141. -(UIColor *)selectColor
  142. {
  143. if (_selectColor == nil) {
  144. return [UIColor blueColor];
  145. }
  146. return _selectColor;
  147. }
  148. /*
  149. // Only override drawRect: if you perform custom drawing.
  150. // An empty implementation adversely affects performance during animation.
  151. - (void)drawRect:(CGRect)rect {
  152. // Drawing code
  153. }
  154. */
  155. @end