SelecterToolsScrolView.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // SelecterToolsScrolView.m
  3. // SelecterTools
  4. //
  5. // Created by zhao on 16/3/15.
  6. // Copyright © 2016年 xincheng. All rights reserved.
  7. //
  8. #import "SelecterToolsScrolView.h"
  9. #define WIDTH [UIScreen mainScreen].bounds.size.width
  10. #define HEIGHT [UIScreen mainScreen].bounds.size.height
  11. #define TitleFont 15
  12. @interface SelecterToolsScrolView()
  13. @property(nonatomic,copy)BtnClick btnClick;
  14. @property(nonatomic,retain)NSMutableArray *btnArr;
  15. @property(nonatomic,retain)UIButton * previousBtn;
  16. @property(nonatomic,retain)UIButton * currentBtn;
  17. @property(nonatomic,retain)UIView *bottomScrLine;
  18. @end
  19. @implementation SelecterToolsScrolView
  20. /*
  21. // Only override drawRect: if you perform custom drawing.
  22. // An empty implementation adversely affects performance during animation.
  23. - (void)drawRect:(CGRect)rect {
  24. // Drawing code
  25. }
  26. */
  27. -(instancetype)initWithSeleterConditionTitleArr:(NSArray *)titleArr andBtnBlock:(BtnClick)btnClick
  28. {
  29. self = [super init];
  30. if (self) {
  31. self.frame = CGRectMake(-1,NavHeader,WIDTH-40, 40);
  32. self.backgroundColor = NewThemeColor;
  33. _btnArr = [NSMutableArray array];
  34. for (int i = 0; i<titleArr.count; i++) {
  35. UIButton *titleBtn = [[UIButton alloc]initWithFrame:CGRectMake(i*80,0, 80,40)];
  36. titleBtn.tag = i;
  37. [titleBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  38. [titleBtn setTitle:titleArr[i] forState:UIControlStateNormal];
  39. titleBtn.titleLabel.font = [UIFont systemFontOfSize:TitleFont];
  40. [titleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  41. [titleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
  42. [self addSubview:titleBtn];
  43. [_btnArr addObject:titleBtn];
  44. if (i == 0) {
  45. _previousBtn = titleBtn;
  46. _currentBtn = titleBtn;
  47. titleBtn.selected = YES;
  48. }
  49. }
  50. _bottomScrLine = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-4,80,2)];
  51. _bottomScrLine.center = CGPointMake(_currentBtn.center.x, _bottomScrLine.center.y);
  52. _bottomScrLine.backgroundColor = [UIColor whiteColor];
  53. [self addSubview:_bottomScrLine];
  54. self.contentSize = CGSizeMake(titleArr.count*80, 30);
  55. self.showsHorizontalScrollIndicator = NO;
  56. self.showsVerticalScrollIndicator = NO;
  57. // self.layer.borderWidth = 0.5;
  58. // self.layer.borderColor = [UIColor lightGrayColor].CGColor;
  59. self.btnClick = btnClick;
  60. }
  61. return self;
  62. }
  63. -(void)updateSelecterToolsIndex:(NSInteger )index
  64. {
  65. UIButton *selectBtn = _btnArr[index];
  66. [self changeSelectBtn:selectBtn];
  67. }
  68. -(void)btnClick:(UIButton *)sender
  69. {
  70. // [self changeSelectBtn:sender];
  71. self.btnClick(sender);
  72. }
  73. -(void)changeSelectBtn:(UIButton *)btn
  74. {
  75. _previousBtn = _currentBtn;
  76. _currentBtn = btn;
  77. _previousBtn.selected = NO;
  78. _currentBtn.selected = YES;
  79. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
  80. _bottomScrLine.center = CGPointMake(_currentBtn.center.x, _bottomScrLine.center.y);
  81. } completion:^(BOOL finished) {
  82. }];
  83. if (_currentBtn.center.x<((WIDTH/2)-20)) {
  84. [self setContentOffset:CGPointMake(0, 0) animated:YES];
  85. }else if (_currentBtn.center.x>self.contentSize.width-((WIDTH/2)-20))
  86. {
  87. [self setContentOffset:CGPointMake(self.contentSize.width-(WIDTH-40), 0) animated:YES];
  88. }else
  89. {
  90. [self setContentOffset:CGPointMake(btn.center.x-((WIDTH/2)-20), 0) animated:YES];
  91. }
  92. }
  93. //
  94. //-(CGFloat)getTitleContentWidth:(NSString *)title
  95. //{
  96. // CGRect rect = [title boundingRectWithSize:CGSizeMake(WIDTH, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:TitleFont]} context:nil];
  97. // return rect.size.width;
  98. //}
  99. @end