// // TabControl.m // model // // Created by Drew on 2018/11/1. // Copyright © 2018年 Mine. All rights reserved. // #import "TabControl.h" @interface TabControl() @property (weak, nonatomic) IBOutlet UIView *indicator; @property (weak, nonatomic) IBOutlet UILabel *label0; @property (weak, nonatomic) IBOutlet UILabel *label1; @property (nonatomic, strong) UIView* view; @end @implementation TabControl - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self initView]; } return self; } - (instancetype)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { [self initView]; } return self; } - (void) initView { NSBundle *bundle = [NSBundle bundleForClass:[self class]]; NSString *className = NSStringFromClass([self class]); UIView *view = [[bundle loadNibNamed:className owner:self options:nil] firstObject]; view.frame = self.bounds; view.backgroundColor = [UIColor clearColor]; [self addSubview:view]; self.clipsToBounds = YES; _view = view; _index = 0; _normalColor = [UIColor blackColor]; _activeColor = [UIColor whiteColor]; } -(void)layoutSubviews { self.layer.cornerRadius = self.frame.size.height / 2; self.indicator.layer.cornerRadius = self.frame.size.height / 2; } - (void)setIndex:(NSInteger)index { if(_index != index) { [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self.indicator.transform = CGAffineTransformMakeTranslation(self.indicator.frame.size.width * index, 0); if(index == 0) { self.label0.textColor = self.activeColor; self.label1.textColor = self.normalColor; } else { self.label0.textColor = self.normalColor; self.label1.textColor = self.activeColor; } } completion:nil]; } _index = index; } - (IBAction)tapTab:(id)sender { NSInteger index = ((UITapGestureRecognizer*)sender).view.tag; if(self.index != index) { [self setIndex:index]; if (self.delegate) { [self.delegate tabChange:index]; } } } - (void)setBackgroungColor:(UIColor *)backgroungColor { self.view.backgroundColor = backgroungColor; } - (void)setIndicatorColor:(UIColor *)indicatorColor { self.indicator.backgroundColor = indicatorColor; } -(void)setColor:(UIColor *)color forState:(TabControlState)state { switch (state) { case TabControlStateNormal: self.normalColor = color; break; case TabControlStateActive: self.activeColor = color; break; default: break; } } - (void)setActiveColor:(UIColor *)activeColor { _activeColor = activeColor; if(self.index == 0) { self.label0.textColor = self.activeColor; self.label1.textColor = self.normalColor; } else { self.label0.textColor = self.normalColor; self.label1.textColor = self.activeColor; } } - (void)setNormalColor:(UIColor *)normalColor { _normalColor = normalColor; if(self.index == 0) { self.label0.textColor = self.activeColor; self.label1.textColor = self.normalColor; } else { self.label0.textColor = self.normalColor; self.label1.textColor = self.activeColor; } } - (void)setTab1:(NSString *)tab1 { self.label0.text = tab1; } - (void)setTab2:(NSString *)tab2 { self.label1.text = tab2; } @end