CustomSegView.m 2.76 KB
Newer Older
陈俊俊's avatar
陈俊俊 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
//
//  CustomSegView.m
//  XFFruit
//
//  Created by 陈俊俊 on 15/11/6.
//  Copyright © 2015年 Xummer. All rights reserved.
//

#import "CustomSegView.h"
#define BeginBtnTag  2000000

@interface CustomSegView ()
{
    UIView *_bgView;
    NSInteger _currentBtnTag;
}
@property (nonatomic,copy)NSArray *segArr;
@end



@implementation CustomSegView
- (instancetype)initWithFrame:(CGRect)frame withArr:(NSArray *)arr{
    self = [super initWithFrame:frame];
    if (self) {
        self.segArr = arr;
        [self bulidLayout];
    }
    return self;
}

- (void)bulidLayout{
    _bgView = [[UIView alloc]initWithFrame:self.bounds];
    _bgView.layer.borderWidth = 1;
陈俊俊's avatar
陈俊俊 committed
35
    _bgView.layer.borderColor = ReportColor.CGColor;
陈俊俊's avatar
陈俊俊 committed
36 37 38 39 40 41 42 43 44 45 46
    _bgView.layer.cornerRadius = 5;
    _bgView.layer.masksToBounds = YES;
    [self addSubview:_bgView];
    _bgView.clipsToBounds = YES;
    _currentBtnTag = 0;
    CLog(@"%f",_bgView.width);
    CGFloat width = _bgView.width/self.segArr.count;
    for (NSInteger i = 0; i < self.segArr.count; i++) {
        IBTCustomButtom *segBtn = [IBTCustomButtom buttonWithType:UIButtonTypeCustom];
        segBtn.frame = CGRectMake(width*i, 0, width, _bgView.height);
        segBtn.titleLabel.font = GXF_FIFTEENTEN_SIZE;
陈俊俊's avatar
陈俊俊 committed
47
        [segBtn setTitleColor:ReportColor forState:UIControlStateNormal];
陈俊俊's avatar
陈俊俊 committed
48 49 50 51 52
        segBtn.tag = BeginBtnTag + i;
        [segBtn setTitle:self.segArr[i] forState:UIControlStateNormal];
        [segBtn addTarget:self action:@selector(segClick:) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:segBtn];
        if (i == 0) {
陈俊俊's avatar
陈俊俊 committed
53
            segBtn.backgroundColor = ReportColor;
陈俊俊's avatar
陈俊俊 committed
54 55 56 57 58
            [segBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            _currentBtnTag = i+ BeginBtnTag;
        }
        
        if (i < self.segArr.count - 1) {
陈俊俊's avatar
陈俊俊 committed
59
            [segBtn addRightBorderWithWidth:1 color:ReportColor];
陈俊俊's avatar
陈俊俊 committed
60 61 62 63 64 65 66 67 68
        }
    }
}
- (void)segClick:(UIButton *)btn{
    if (btn.tag != _currentBtnTag) {
        //获取上一个Btn
        IBTCustomButtom *beforeBtn = (IBTCustomButtom *)[_bgView viewWithTag:_currentBtnTag];
        if ([beforeBtn isKindOfClass:[UIButton class]] && beforeBtn) {
            beforeBtn.backgroundColor = RGBA(1, 1, 1, 0);
陈俊俊's avatar
陈俊俊 committed
69
            [beforeBtn setTitleColor:ReportColor forState:UIControlStateNormal];
陈俊俊's avatar
陈俊俊 committed
70
        }
陈俊俊's avatar
陈俊俊 committed
71
        btn.backgroundColor = ReportColor;
陈俊俊's avatar
陈俊俊 committed
72 73 74 75 76 77 78 79 80 81 82 83
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _currentBtnTag = btn.tag;
        if ([self.delegate respondsToSelector:@selector(customSegOneClick:)]) {
            [self.delegate customSegOneClick:btn.titleLabel.text];
        }
    }else{
        if ([self.delegate respondsToSelector:@selector(customSegTwoClick:)]) {
            [self.delegate customSegTwoClick:btn.titleLabel.text];
        }
    }
}
@end