// // FKSegmentControl.m // FirstLink // // Created by jack on 16/5/10. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKSegmentControl.h" @interface FKSegmentItemView : UIControl @property (nonatomic, strong) UILabel *titleLabel; @end @implementation FKSegmentItemView - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self addAllSubviews]; } return self; } - (void)addAllSubviews{ [self addSubview:self.titleLabel]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self); }]; } - (void)setSelected:(BOOL)selected{ [super setSelected:selected]; if (selected) { self.backgroundColor = UIColorFromRGB(0xccccccc); // self.titleLabel.textColor = UIColorFromRGB(0x4a4a4a); }else{ self.backgroundColor = UIColorFromRGB(0xffffff); // self.titleLabel.textColor = UIColorFromRGB(0x9b9b9b); } } - (UILabel *)titleLabel{ if (_titleLabel == nil) { _titleLabel = [[UILabel alloc]init]; _titleLabel.font = [UIFont systemFontOfSize:14]; _titleLabel.textColor = UIColorFromRGB(0x4a4a4a); } return _titleLabel; } @end //////////////////////////////////////////////////////////////////////////////////////////////////////////// @interface FKSegmentControl () @property (nonatomic, strong) NSMutableArray *itemViewArrayM; @end @implementation FKSegmentControl - (instancetype)initWithIndexTitles:(NSArray *)titleArray{ if (self = [super init]) { self.titleArray = titleArray; self.layer.cornerRadius = 3.0f; self.layer.borderColor = UIColorFromRGB(0xccccccc).CGColor; self.layer.borderWidth = 0.5; self.clipsToBounds = YES; [self buildSubviewWithTitleArray:self.titleArray]; } return self; } - (void)buildSubviewWithTitleArray:(NSArray *)titleArray{ if (titleArray.count == 0) return; for (UIView *view in self.subviews) { if ([view isKindOfClass:[FKSegmentItemView class]]){ [view removeFromSuperview]; } } for (FKSegmentItemView *itemView in self.itemViewArrayM) { [itemView setSelected:NO]; } while (self.itemViewArrayM.count < titleArray.count) { FKSegmentItemView *itemView = [[FKSegmentItemView alloc]init]; [itemView addTarget:self action:@selector(clickItemView:) forControlEvents:UIControlEventTouchUpInside]; [self.itemViewArrayM addObject:itemView]; } UIView *uperView = nil; for (NSInteger i = 0; i < titleArray.count; i++) { FKSegmentItemView *itemView = self.itemViewArrayM[i]; itemView.titleLabel.text = titleArray[i]; itemView.selected = NO; if (i == self.defaultSelectIndex) itemView.selected = YES; [self addSubview:itemView]; [itemView mas_makeConstraints:^(MASConstraintMaker *make) { if (i == 0){ make.left.equalTo(self); }else if (i == titleArray.count - 1){ make.left.equalTo(uperView.mas_right); make.right.equalTo(self); }else{ make.left.equalTo(uperView.mas_right); make.right.equalTo(self); } if (uperView){ make.width.equalTo(uperView); } make.top.bottom.equalTo(self); }]; uperView = itemView; } } - (void)clickItemView:(FKSegmentItemView *)sender{ if (sender.selected) return; [self resetAllSelectedState]; sender.selected = YES; NSUInteger index = [self.itemViewArrayM indexOfObject:sender]; if (self.changeIndex){ self.changeIndex(index); } } - (void)resetAllSelectedState{ for (FKSegmentItemView *itemView in self.subviews) { if ([itemView isKindOfClass:[FKSegmentItemView class]]){ itemView.selected = NO; } } } - (NSMutableArray *)itemViewArrayM{ if (_itemViewArrayM == nil) { _itemViewArrayM = [[NSMutableArray alloc]init]; } return _itemViewArrayM; } @end