123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- //
- // 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
|