// // IifshSegmentView.m // CollectionViewTest // // Created by imac on 17/3/14. // Copyright © 2017年 xiang. All rights reserved. // #import "IifshSegmentView.h" #define SEG_TITLE_FONTCOLOR RGB(38, 192, 238) #define SEG_LINE_FONTCOLOR RGB(0, 185.0, 239.0) #define SEG_TITLE_FONTNORMAllCOLOR RGB(51.0, 51.0, 51.0) @interface IifshSegmentView() @property (nonatomic,strong) UIView *BootomLine; @property (nonatomic,strong) UIButton *leftBtn; @property (nonatomic,strong) UIButton *rightBtn; @property (nonatomic) NSInteger selectIndex; @property (nonatomic,copy) NSString *leftTitle; @property (nonatomic,copy) NSString *rightTitle; @property (nonatomic,strong) UIView *lineView; @end @implementation IifshSegmentView -(instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self creatSubs]; self.backgroundColor = [UIColor whiteColor]; } return self; } -(void)creatSubs { CGFloat BtnW = (self.frame.size.width-2)/2; _leftBtn =[UIButton buttonWithType:UIButtonTypeCustom]; _leftBtn.frame = CGRectMake(0, 0,BtnW, self.frame.size.height); [_leftBtn addTarget:self action:@selector(leftBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:_leftBtn]; CGFloat lineW = 79.0*KWidth_Scale; _BootomLine = [[UIView alloc] initWithFrame:CGRectMake(BtnW/2 - lineW/2, self.frame.size.height -2, lineW, 2)]; _BootomLine.backgroundColor = SEG_LINE_FONTCOLOR; [self addSubview:_BootomLine]; [self bringSubviewToFront:_BootomLine]; _lineView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_leftBtn.frame),6,1, self.frame.size.height - 6 -2)]; _lineView.backgroundColor = [UIColor lightGrayColor]; [self addSubview:_lineView]; _rightBtn =[UIButton buttonWithType:UIButtonTypeCustom]; _rightBtn.frame = CGRectMake(CGRectGetMaxX(_lineView.frame), 0,BtnW, self.frame.size.height); [_rightBtn addTarget:self action:@selector(rightBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:_rightBtn]; } -(void)rightBtnAction:(UIButton*)btn{ if (_selectIndex ==1) { return; } if (self.segDelegate) { if (self.isSelectStyle) { [self setLeftTitleColor:SEG_TITLE_FONTNORMAllCOLOR rightTilteColor:SEG_TITLE_FONTCOLOR]; } CGFloat lineW = 79.0*KWidth_Scale; _BootomLine.frame =CGRectMake(CGRectGetMidX(btn.frame) - lineW/2, self.frame.size.height -2, lineW, 2); [self.segDelegate segmentSelectAtIndext:1]; _selectIndex = 1; } } -(void)leftBtnAction:(UIButton *)btn{ if (_selectIndex ==0) { return; } if (self.segDelegate) { if (self.isSelectStyle) { [self setLeftTitleColor:SEG_TITLE_FONTCOLOR rightTilteColor:SEG_TITLE_FONTNORMAllCOLOR]; } CGFloat BtnW = (self.frame.size.width-2)/2; CGFloat lineW = 79.0*KWidth_Scale; _BootomLine.frame =CGRectMake(BtnW/2 - lineW/2, self.frame.size.height -2, lineW, 2); [self.segDelegate segmentSelectAtIndext:0]; _selectIndex = 0; } } -(void)initleftTitle:(NSString *)leftTitle addRightTitle:(NSString *)rightTitle isSelectStyle:(BOOL)isSelectStyle{ self.isSelectStyle = isSelectStyle; _rightBtn.titleLabel.font = [UIFont systemFontOfSize:15]; [_rightBtn setTitle:rightTitle forState:UIControlStateNormal]; [_rightBtn setTitleColor:SEG_TITLE_FONTNORMAllCOLOR forState:UIControlStateNormal]; _leftBtn.titleLabel.font = [UIFont systemFontOfSize:15]; [_leftBtn setTitle:leftTitle forState:UIControlStateNormal]; [_leftBtn setTitleColor:SEG_TITLE_FONTNORMAllCOLOR forState:UIControlStateNormal]; _rightTitle = rightTitle; _leftTitle = leftTitle; // 默认选中左侧 if (isSelectStyle) { [self setLeftTitleColor:SEG_TITLE_FONTCOLOR rightTilteColor:SEG_TITLE_FONTNORMAllCOLOR]; //隐藏中间line 暂用 isSelectStyle 判断 _lineView.hidden = YES; } } -(void)setLeftTitleColor:(UIColor*)leftTitleColor rightTilteColor:(UIColor *)rightTitleColor { [_rightBtn setTitleColor:rightTitleColor forState:UIControlStateNormal]; [_leftBtn setTitleColor:leftTitleColor forState:UIControlStateNormal]; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end