JLChen
2021-11-04 1443556e9ccb1a19ed8e6710c16c8adc4d4f4fb3
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
//  Copyright © 2019 dahua. All rights reserved.
//
 
#import "LCSegmentController.h"
#import "LCUIKit.h"
 
@interface LCSegmentController ()
 
/// 回调
@property (copy, nonatomic) selectBlock block;
 
/// 回调
@property (strong, nonatomic) NSMutableArray *array;
 
/// 回调
@property (strong, nonatomic) UIView *selectedView;
 
/// 默认选中项
@property (nonatomic) NSInteger defaultSelect;
 
@end
 
@implementation LCSegmentController
 
//MARK: - Public Methods
+ (instancetype)segmentWithItems:(NSArray<NSString *> *)items SelectedBlock:(void (^)(NSUInteger))selected {
    LCSegmentController *segment = [[LCSegmentController alloc] init];
    [segment setValue:items forKey:@"items"];
    [segment setValue:selected forKey:@"block"];
    [segment setUpView];
    return segment;
}
 
+ (instancetype)segmentWithFrame:(CGRect)frame DefaultSelect:(NSInteger)select Items:(NSArray<NSString *> *)items SelectedBlock:(selectBlock)selected {
    LCSegmentController *segment = [[LCSegmentController alloc] initWithFrame:frame];
    segment.defaultSelect = select;
    [segment setValue:items forKey:@"items"];
    [segment setValue:selected forKey:@"block"];
    [segment setUpView];
    return segment;
}
 
//MARK: - Private Methods
- (NSMutableArray *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}
 
- (void)setUpView {
    CGFloat itemWidth = self.bounds.size.width / self.items.count;//宽度
    self.selectedView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, itemWidth, self.bounds.size.height)];
    [self addSubview:self.selectedView];
    self.selectedView.backgroundColor = [UIColor dhcolor_c10];
    
    for (int a = 0; a < self.items.count; a++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:self.items[a] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont lcFont_t6];
        btn.titleLabel.adjustsFontSizeToFitWidth = YES;
        [btn addTarget:self action:@selector(textSelect:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = a;
        [btn setTitleColor:[UIColor dhcolor_c10] forState:UIControlStateNormal];
        [self.array addObject:btn];
        [self addSubview:btn];
        [btn setFrame:CGRectMake(itemWidth*a,0,itemWidth, self.bounds.size.height)];
//        if (a==self.defaultSelect) {
//            [self textSelect:btn];
//        }
    }
    [self setSelectIndex:self.defaultSelect];
    
    self.layer.cornerRadius = self.bounds.size.height / 2.0;
    self.layer.masksToBounds = YES;
    [self setBackgroundColor:[UIColor whiteColor]];
    self.layer.borderColor = [UIColor dhcolor_c10].CGColor;
    self.layer.borderWidth = 1.f;
    self.selectedView.layer.cornerRadius = self.selectedView.bounds.size.height / 2.0;
    self.selectedView.layer.masksToBounds = YES;
    
}
 
- (void)textSelect:(UIButton *)btn {
    for (UIButton * btn in self.array) {
        [btn setTitleColor:[UIColor dhcolor_c10] forState:UIControlStateNormal];
    }
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [UIView animateWithDuration:0.4 animations:^{
        self.selectedView.center = btn.center;
    } completion:^(BOOL finished) {
        if (self.block) {
            self.block(btn.tag);
        }
    }];
}
 
- (void)setSelectIndex:(NSInteger)index {
    UIButton *selectBtn = [self.array objectAtIndex:index];
    for (UIButton *btn in self.array) {
        [btn setTitleColor:[UIColor dhcolor_c10] forState:UIControlStateNormal];
    }
    [selectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [UIView animateWithDuration:0.4 animations:^{
        self.selectedView.center = selectBtn.center;
    }];
}
 
- (void)setEnable:(BOOL)enable {
    _enable = enable;
    if (enable) {
        self.alpha = 1;
        self.userInteractionEnabled = YES;
    } else {
        self.alpha = 0.7;
        self.userInteractionEnabled = NO;
    }
}
 
@end