ModelTitleView.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // ModelTitleView.m
  3. // model
  4. //
  5. // Created by JuYi on 2018/7/17.
  6. // Copyright © 2018年 Mine. All rights reserved.
  7. // 选择按钮
  8. #import "ModelTitleView.h"
  9. @interface ModelTitleView()<UIScrollViewDelegate>
  10. @property (nonatomic, strong) NSMutableArray *titleBettons;
  11. @property (nonatomic, weak) UIScrollView *titleScrollView;
  12. @property (nonatomic, weak) UIButton *selectBtn;
  13. @property (nonatomic, strong) UIView *lineView; //下划线
  14. @end
  15. @implementation ModelTitleView
  16. - (NSMutableArray *)titleBettons {
  17. if (!_titleBettons) {
  18. _titleBettons = [NSMutableArray arrayWithCapacity:0];
  19. }
  20. return _titleBettons;
  21. }
  22. - (instancetype)initWithFrame:(CGRect)frame {
  23. self = [super initWithFrame:frame];
  24. if (self) {
  25. self.backgroundColor = [UIColor whiteColor];
  26. }
  27. return self;
  28. }
  29. - (void)setTitleArr:(NSArray *)titleArr {
  30. _titleArr = titleArr;
  31. [self setUpAllChildTitle];
  32. }
  33. /**
  34. 添加字标题
  35. */
  36. - (void)setUpAllChildTitle {
  37. CGFloat btnh = self.bounds.size.height;
  38. //
  39. UIScrollView *titleScrollView = [[UIScrollView alloc] init];
  40. titleScrollView.frame = CGRectMake(0, 0, ScreenWidth,btnh);
  41. // titleScrollView.delegate = self;
  42. [self addSubview:titleScrollView];
  43. self.titleScrollView = titleScrollView;
  44. //
  45. NSInteger count = self.titleArr.count;
  46. CGFloat btnx = 0;
  47. CGFloat btny = 0;
  48. CGFloat btnw = ScreenWidth / count;
  49. for (NSInteger i = 0; i < count; i++) {
  50. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  51. [btn setTitle:self.titleArr[i] forState:UIControlStateNormal];
  52. btnx = btnw * i;
  53. btn.frame = CGRectMake(btnx, btny, btnw, btnh);
  54. btn.tag = i;
  55. [btn setTitleColor:RGBValueColor(0x333333, 1.0) forState:UIControlStateNormal];
  56. btn.titleLabel.font = [UIFont systemFontOfSize:15];
  57. [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  58. if (i == 0) {
  59. [self btnClick:btn];
  60. }
  61. [self.titleBettons addObject:btn];
  62. [self.titleScrollView addSubview:btn];
  63. }
  64. // CGFloat contentWidth = ScreenWidth < 375 ? 375 : ScreenWidth;
  65. // self.titleScrollView.contentSize = CGSizeMake(contentWidth, 0);
  66. // self.titleScrollView.showsHorizontalScrollIndicator = NO;
  67. self.lineView = [[UIView alloc] init];
  68. self.lineView.frame = CGRectMake(0, 0, 35, 2);
  69. self.lineView.backgroundColor = RGBValueColor(0xfe3f87, 1.0);
  70. UIButton *button = self.titleBettons[0];
  71. self.lineView.center = CGPointMake(button.center.x, btnh - 2);
  72. [self.titleScrollView addSubview:self.lineView];
  73. //
  74. self.longLineView = [[UIImageView alloc] initWithFrame:CGRectMake(0, btnh - 1, ScreenWidth, 1)];
  75. self.longLineView.image = [UIImage imageNamed:@"fengexian"];
  76. [self.titleScrollView addSubview:self.longLineView];
  77. }
  78. - (void)btnClick:(UIButton *)sender {
  79. [_selectBtn setTitleColor:RGBValueColor(0x333333, 1.0) forState:UIControlStateNormal];
  80. [sender setTitleColor:RGBValueColor(0xfe3f87, 1.0) forState:UIControlStateNormal];
  81. self.lineView.center = CGPointMake(sender.center.x, self.frame.size.height - 2);
  82. _selectBtn = sender;
  83. if (self.ModelTitleViewBlock) {
  84. self.ModelTitleViewBlock(sender.tag);
  85. }
  86. }
  87. @end