TabControl.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // TabControl.m
  3. // model
  4. //
  5. // Created by Drew on 2018/11/1.
  6. // Copyright © 2018年 Mine. All rights reserved.
  7. //
  8. #import "TabControl.h"
  9. @interface TabControl()
  10. @property (weak, nonatomic) IBOutlet UIView *indicator;
  11. @property (weak, nonatomic) IBOutlet UILabel *label0;
  12. @property (weak, nonatomic) IBOutlet UILabel *label1;
  13. @property (nonatomic, strong) UIView* view;
  14. @end
  15. @implementation TabControl
  16. - (instancetype)initWithFrame:(CGRect)frame {
  17. if (self = [super initWithFrame:frame]) {
  18. [self initView];
  19. }
  20. return self;
  21. }
  22. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  23. if (self = [super initWithCoder:aDecoder]) {
  24. [self initView];
  25. }
  26. return self;
  27. }
  28. - (void) initView {
  29. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  30. NSString *className = NSStringFromClass([self class]);
  31. UIView *view = [[bundle loadNibNamed:className owner:self options:nil] firstObject];
  32. view.frame = self.bounds;
  33. view.backgroundColor = [UIColor clearColor];
  34. [self addSubview:view];
  35. self.clipsToBounds = YES;
  36. _view = view;
  37. _index = 0;
  38. _normalColor = [UIColor blackColor];
  39. _activeColor = [UIColor whiteColor];
  40. }
  41. -(void)layoutSubviews {
  42. self.layer.cornerRadius = self.frame.size.height / 2;
  43. self.indicator.layer.cornerRadius = self.frame.size.height / 2;
  44. }
  45. - (void)setIndex:(NSInteger)index {
  46. if(_index != index) {
  47. [UIView animateWithDuration:0.25
  48. delay:0
  49. options:UIViewAnimationOptionCurveEaseOut
  50. animations:^{
  51. self.indicator.transform = CGAffineTransformMakeTranslation(self.indicator.frame.size.width * index, 0);
  52. if(index == 0) {
  53. self.label0.textColor = self.activeColor;
  54. self.label1.textColor = self.normalColor;
  55. } else {
  56. self.label0.textColor = self.normalColor;
  57. self.label1.textColor = self.activeColor;
  58. }
  59. }
  60. completion:nil];
  61. }
  62. _index = index;
  63. }
  64. - (IBAction)tapTab:(id)sender {
  65. NSInteger index = ((UITapGestureRecognizer*)sender).view.tag;
  66. if(self.index != index) {
  67. [self setIndex:index];
  68. if (self.delegate) {
  69. [self.delegate tabChange:index];
  70. }
  71. }
  72. }
  73. - (void)setBackgroungColor:(UIColor *)backgroungColor {
  74. self.view.backgroundColor = backgroungColor;
  75. }
  76. - (void)setIndicatorColor:(UIColor *)indicatorColor {
  77. self.indicator.backgroundColor = indicatorColor;
  78. }
  79. -(void)setColor:(UIColor *)color forState:(TabControlState)state {
  80. switch (state) {
  81. case TabControlStateNormal:
  82. self.normalColor = color;
  83. break;
  84. case TabControlStateActive:
  85. self.activeColor = color;
  86. break;
  87. default:
  88. break;
  89. }
  90. }
  91. - (void)setActiveColor:(UIColor *)activeColor {
  92. _activeColor = activeColor;
  93. if(self.index == 0) {
  94. self.label0.textColor = self.activeColor;
  95. self.label1.textColor = self.normalColor;
  96. } else {
  97. self.label0.textColor = self.normalColor;
  98. self.label1.textColor = self.activeColor;
  99. }
  100. }
  101. - (void)setNormalColor:(UIColor *)normalColor {
  102. _normalColor = normalColor;
  103. if(self.index == 0) {
  104. self.label0.textColor = self.activeColor;
  105. self.label1.textColor = self.normalColor;
  106. } else {
  107. self.label0.textColor = self.normalColor;
  108. self.label1.textColor = self.activeColor;
  109. }
  110. }
  111. - (void)setTab1:(NSString *)tab1 {
  112. self.label0.text = tab1;
  113. }
  114. - (void)setTab2:(NSString *)tab2 {
  115. self.label1.text = tab2;
  116. }
  117. @end