ZXWSegment.m
3.59 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
//
// ZXWSegment.m
// LoongsCity
//
// Created by loongs_zxw on 2017/11/15.
// Copyright © 2017年 xiaoxiong. All rights reserved.
//
#import "ZXWSegment.h"
#import "ZXWKitConst.h"
@interface ZXWSegment ()
@property (strong, nonatomic) UILabel * titleLabel;
@end
@implementation ZXWSegment
- (void)layoutSubviews {
[super layoutSubviews];
if (self.selectProgress == 1) {
self.currentStatus = segementItemCurrentStatusSelect;
}
}
/**
* 正常标题颜色
*/
- (void)setTitleNormalColor:(UIColor *)titleNormalColor {
_titleNormalColor = titleNormalColor;
self.titleLabel.textColor = titleNormalColor;
}
- (void)setTitle:(NSString *)title {
if (_title.length == 0) {
// 初始化
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
self.titleLabel.font = segementTitleNormalFont;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.textColor = self.titleNormalColor;
[self addSubview:self.titleLabel];
}
self.titleLabel.text = title;
}
- (void)setSelectProgress:(CGFloat)selectProgress {
_selectProgress = selectProgress;
// 判断是正常状态、选中状态、正在选中状态、取消选中状态
if (_selectProgress == 0) {
// 正常状态
self.currentStatus = segementItemCurrentStatusNormal;
} else if (_selectProgress == 1) {
// 选中状态
self.currentStatus = segementItemCurrentStatusSelect;
} else if (_selectProgress < _selectProgress) {
// 正在选中状态
self.currentStatus = segementItemCurrentStatusSelecting;
} else {
// 取消选中状态
self.currentStatus = segementItemCurrentStatusDeselecting;
}
}
/**
* 设置当前状态
*/
- (void)setCurrentStatus:(SegementItemCurrentStatus)currentStatus {
_currentStatus = currentStatus;
if (currentStatus == segementItemCurrentStatusNormal) {
// 正常状态
// 设置正常时的标题颜色
self.titleLabel.textColor = self.titleNormalColor;
// 设置正常时的背景颜色
self.backgroundColor = self.normalColor;
// 设置正常时的标题颜色
// if (self.titleSelectBold == NO) {
// self.titleLabel.font = segementTitleNormalFont;
// }
self.titleLabel.font = segementTitleNormalFont;
self.titleLabel.transform = CGAffineTransformMakeScale(1, 1);
} else if (currentStatus == segementItemCurrentStatusSelect) {
// 选中状态
[self titleScale];
// 设置选中时的标题颜色
self.titleLabel.textColor = self.titleSelectColor;
// 设置选中时的背景颜色
self.backgroundColor = self.selectColor;
// 设置选中时的标题颜色
if (self.titleSelectBold == YES) {
self.titleLabel.font = segementTitleSelectFont;
}
// 缩放比例
CGFloat scaleRatio = ZXWSegementViewTitleSelectMaxScale;
self.titleLabel.transform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);
} else if (currentStatus == segementItemCurrentStatusDeselecting) {
// 正在取消选中
[self titleScale];
} else {
// 正在选中
[self titleScale];
}
}
- (void)titleScale {
if (self.isTitleScale == NO) {
return;
}
// 缩放比例
CGFloat scaleRatio = 1 + self.selectProgress * (ZXWSegementViewTitleSelectMaxScale - 1);
self.titleLabel.transform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);
}
@end