STSegView.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. #import "STSegView.h"
  3. #define lineColor [UIColor colorWithWhite:.3 alpha:.1]
  4. #define subTitleColor [UIColor colorWithWhite:.3 alpha:1]
  5. @interface STSegView()
  6. {
  7. NSMutableArray* btns;
  8. UIView *bar;
  9. }
  10. @end
  11. @implementation STSegView
  12. -(instancetype)initWithFrame:(CGRect)frame
  13. {
  14. self = [super initWithFrame:frame];
  15. if (self) {
  16. _colorNormal = subTitleColor;
  17. _colorSelected = RQMianColor;
  18. _barColor = RQMianColor;
  19. _font = 14;
  20. btns = [NSMutableArray array];
  21. [self addSelfViewWithRect:CGRectMake(0, self.height, self.width, 1) Color:lineColor];
  22. bar = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-2, self.width, 3)];
  23. [self addSubview:bar];
  24. }
  25. return self;
  26. }
  27. -(void)setTitles:(NSArray *)titles
  28. {
  29. if (!titles) {
  30. return;
  31. }
  32. UIButton* btn;
  33. /**这里只需要保证。btn的数量足够即可。
  34. */
  35. NSInteger tag = btns.count;
  36. for (int i=0; i<(titles.count - _titles.count); i++) {
  37. btn = [[UIButton alloc] init];
  38. [self addSubview:btn];
  39. [btns addObject:btn];
  40. [btn target:self];
  41. [btn setTag:tag++];
  42. }
  43. bar.width = self.width / titles.count;
  44. _titles = titles;
  45. }
  46. -(void)layoutSubviews
  47. {
  48. [super layoutSubviews];
  49. if (!_titles) {
  50. return;
  51. }
  52. [bar setBackgroundColor:_barColor];
  53. CGFloat y,w,h;
  54. w = self.width * 1.0 / _titles.count;
  55. h = self.height;
  56. y = 0;
  57. UIButton* btn;
  58. for (int i =0; i < _titles.count; i++) {
  59. btn = btns[i];
  60. [btn setFrame:CGRectMake(w*i, y, w, h)];
  61. [btn setTitleNormal:_titles[i]];
  62. [btn setTitle:_titles[i] textColor:_colorNormal font:_font fotState:UIControlStateNormal];
  63. [btn setTitleColor:_colorSelected forState:UIControlStateSelected];
  64. }
  65. }
  66. -(void)click:(MyBlockType)block
  67. {
  68. clkBlock = block;
  69. }
  70. -(void)setSelectedIndex:(NSInteger)selectedIndex
  71. {
  72. if (selectedIndex > _titles.count -1) {
  73. selectedIndex = _titles.count -1;
  74. }
  75. if (selectedIndex < 0 ) {
  76. selectedIndex = 0;
  77. }
  78. _selectedIndex = selectedIndex;
  79. if (clkBlock) {
  80. clkBlock([NSString stringWithFormat:@"%d",(int)selectedIndex]);
  81. }else if ([_delegate respondsToSelector:@selector(didSelectedIndex:)]) {
  82. [_delegate didSelectedIndex:selectedIndex];
  83. }
  84. if (!_titles || _titles.count < 1) {
  85. return;
  86. }
  87. UIButton* btn ;
  88. for (btn in btns) {
  89. [btn setSelected:NO];
  90. }
  91. btn = btns[selectedIndex];
  92. [btn setSelected:YES];
  93. [UIView animateWithDuration:.15 animations:^{
  94. bar.x = selectedIndex * bar.width;
  95. }];
  96. }
  97. -(void)btnClick:(UIButton*)sender
  98. {
  99. int tag = (int)sender.tag;
  100. self.selectedIndex = tag;
  101. }
  102. @end