-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCCSegmentBar.m
More file actions
230 lines (172 loc) · 6.08 KB
/
Copy pathCCSegmentBar.m
File metadata and controls
230 lines (172 loc) · 6.08 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// CCSegmentBar.m
// CCSegmentBar
//
// Created by coderchou on 2016/11/26.
// Copyright © 2016年 coderchou. All rights reserved.
//
#import "CCSegmentBar.h"
#import "UIView+CCSegmentBar.h"
#define kMinMargin 30
@interface CCSegmentBar ()
{
// 记录最后一次点击的按钮
UIButton *_lastBtn;
}
/** 内容承载视图 */
@property (nonatomic, weak) UIScrollView *contentView;
/** 添加的按钮数据 */
@property (nonatomic, strong) NSMutableArray <UIButton *>*itemBtns;
/** 指示器 */
@property (nonatomic, weak) UIView *indicatorView;
@property (nonatomic, strong) CCSegmentBarConfig *config;
@end
@implementation CCSegmentBar
#pragma mark - 接口
+ (instancetype)segmentBarWithFrame: (CGRect)frame {
CCSegmentBar *segmentBar = [[CCSegmentBar alloc] initWithFrame:frame];
// 添加内容承载视图
return segmentBar;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = self.config.segmentBarBackColor;
}
return self;
}
- (void)updateWithConfig: (void(^)(CCSegmentBarConfig *config))configBlock {
if (configBlock) {
configBlock(self.config);
}
// 按照当前的 self.config 进行刷新
self.backgroundColor = self.config.segmentBarBackColor;
for (UIButton *btn in self.itemBtns) {
[btn setTitleColor:self.config.itemNormalColor forState:UIControlStateNormal];
[btn setTitleColor:self.config.itemSelectColor forState:UIControlStateSelected];
btn.titleLabel.font = self.config.itemFont;
}
// 指示器
self.indicatorView.backgroundColor = self.config.indicatorColor;
[self setNeedsLayout];
[self layoutIfNeeded];
}
- (void)setSelectIndex:(NSInteger)selectIndex {
// 数据过滤
if (self.itemBtns.count == 0 || selectIndex < 0 || selectIndex > self.itemBtns.count - 1) {
return;
}
_selectIndex = selectIndex;
UIButton *btn = self.itemBtns[selectIndex];
[self btnClick:btn];
}
- (void)setItems:(NSArray<NSString *> *)items {
_items = items;
// 删除之前添加过多额组件
[self.itemBtns makeObjectsPerformSelector:@selector(removeFromSuperview)];
self.itemBtns = nil;
// 根据所有的选项数据源, 创建Button, 添加到内容视图
for (NSString *item in items) {
UIButton *btn = [[UIButton alloc] init];
btn.tag = self.itemBtns.count;
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
[btn setTitleColor:self.config.itemNormalColor forState:UIControlStateNormal];
[btn setTitleColor:self.config.itemSelectColor forState:UIControlStateSelected];
btn.titleLabel.font = self.config.itemFont;
[btn setTitle:item forState:UIControlStateNormal];
[self.contentView addSubview:btn];
[self.itemBtns addObject:btn];
}
// 手动刷新布局
[self setNeedsLayout];
[self layoutIfNeeded];
}
#pragma mark - 私有方法
- (void)btnClick: (UIButton *)btn {
if ([self.delegate respondsToSelector:@selector(segmentBar:didSelectIndex:fromIndex:)]) {
[self.delegate segmentBar:self didSelectIndex:btn.tag fromIndex:_lastBtn.tag];
}
_selectIndex = btn.tag;
_lastBtn.selected = NO;
btn.selected = YES;
_lastBtn = btn;
[UIView animateWithDuration:0.1 animations:^{
self.indicatorView.width = btn.width + self.config.indicatorExtraW * 2;
self.indicatorView.centerX = btn.centerX;
}];
// 1. 县滚动到btn的位置
CGFloat scrollX = btn.centerX - self.contentView.width * 0.5;
if (scrollX < 0) {
scrollX = 0;
}
if (scrollX > self.contentView.contentSize.width - self.contentView.width) {
scrollX = self.contentView.contentSize.width - self.contentView.width;
}
[self.contentView setContentOffset:CGPointMake(scrollX, 0) animated:YES];
}
#pragma mark - 布局
- (void)layoutSubviews {
[super layoutSubviews];
self.contentView.frame = self.bounds;
// 计算margin
CGFloat totalBtnWidth = 0;
for (UIButton *btn in self.itemBtns) {
[btn sizeToFit];
totalBtnWidth += btn.width;
}
CGFloat caculateMargin = (self.width - totalBtnWidth) / (self.items.count + 1);
if (caculateMargin < kMinMargin) {
caculateMargin = kMinMargin;
}
CGFloat lastX = caculateMargin;
for (UIButton *btn in self.itemBtns) {
// w, h
[btn sizeToFit];
// y 0
// x, y,
btn.y = 0;
btn.x = lastX;
lastX += btn.width + caculateMargin;
}
self.contentView.contentSize = CGSizeMake(lastX, 0);
if (self.itemBtns.count == 0) {
return;
}
UIButton *btn = self.itemBtns[self.selectIndex];
self.indicatorView.width = btn.width + self.config.indicatorExtraW * 2;
self.indicatorView.centerX = btn.centerX;
self.indicatorView.height = self.config.indicatorHeight;
self.indicatorView.y = self.height - self.indicatorView.height;
}
#pragma mark - 懒加载
- (NSMutableArray<UIButton *> *)itemBtns {
if (!_itemBtns) {
_itemBtns = [NSMutableArray array];
}
return _itemBtns;
}
- (UIView *)indicatorView {
if (!_indicatorView) {
CGFloat indicatorH = self.config.indicatorHeight;
UIView *indicatorView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - indicatorH, 0, indicatorH)];
indicatorView.backgroundColor = self.config.indicatorColor;
[self.contentView addSubview:indicatorView];
_indicatorView = indicatorView;
}
return _indicatorView;
}
- (UIScrollView *)contentView {
if (!_contentView) {
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.showsHorizontalScrollIndicator = NO;
[self addSubview:scrollView];
_contentView = scrollView;
}
return _contentView;
}
- (CCSegmentBarConfig *)config {
if (!_config) {
_config = [CCSegmentBarConfig defaultConfig];
}
return _config;
}
@end