STSegView.m 2.7 KB

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