-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGGSwitch.m
219 lines (173 loc) · 7.34 KB
/
GGSwitch.m
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
//
// GGSwitch.m
// GGSwitch
//
// Created by __无邪_ on 15/4/29.
// Copyright (c) 2015年 __无邪_. All rights reserved.
//
#import "GGSwitch.h"
#define degreesToRadians(degrees) ((degrees) * M_PI / 180.0)
#define PROGRESS_LINE_WIDTH 1 //弧线的宽度
static CGFloat kSwitchWidth = 65.0; //宽
static CGFloat kSwitchHeight = 31.0; //高
@interface GGSwitch ()
@property (nonatomic, strong)CAShapeLayer *trackLayer;
@property (nonatomic, unsafe_unretained)BOOL stateisOn;
@end
@implementation GGSwitch{
BOOL shouldAnimated;
UIColor *onColor; //开时的背景色
UIColor *offColor;//关时的背景色
}
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self setFrame:CGRectMake(frame.origin.x, frame.origin.y, kSwitchWidth, kSwitchHeight)];
[self setBackgroundColor:[UIColor whiteColor]];
onColor = [UIColor colorWithRed:0.242 green:0.781 blue:0.126 alpha:1.000];
offColor = [UIColor whiteColor];
CGSize size = CGSizeMake(kSwitchWidth, kSwitchHeight);
_trackLayer = [CAShapeLayer layer];
[self.layer addSublayer:_trackLayer];
_trackLayer.frame = CGRectMake(0, 0, size.height, size.width);
_trackLayer.fillColor = [[UIColor whiteColor] CGColor];
//_trackLayer.strokeColor = [[UIColor greenColor] CGColor];//指定path的渲染颜色
_trackLayer.opacity = 1;
_trackLayer.lineCap = kCALineCapRound;
_trackLayer.lineWidth = PROGRESS_LINE_WIDTH;
_trackLayer.shadowColor = [UIColor grayColor].CGColor;
_trackLayer.shadowOffset = CGSizeMake(0.5, 0);
_trackLayer.shadowOpacity = 1.0;
_trackLayer.shadowRadius = 1.0;
self.stateisOn = NO;
shouldAnimated = YES;
[self addTapGesture];
}
return self;
}
#pragma mark - Missions
- (void)addTapGesture{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(switchStateChanged)];
[self addGestureRecognizer:tapGesture];
}
- (void)switchStateChanged{
self.stateisOn = !self.stateisOn;
CGSize size = CGSizeMake(kSwitchWidth, kSwitchHeight);
CGRect newBounds;
UIColor *color = nil;
if (self.stateisOn) {
newBounds = CGRectMake(size.width - size.height, 0, size.height, size.height);
color = onColor;
}
else{
newBounds = CGRectMake(0, 0, size.height, size.width);
color = offColor;
}
if (shouldAnimated) {
[CATransaction begin];
[CATransaction setDisableActions:!YES];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[CATransaction setAnimationDuration:0.15];
_trackLayer.frame = newBounds;
[CATransaction commit];
}
else{
_trackLayer.frame = newBounds;
}
if (self.tapBlock) {
self.tapBlock(self.stateisOn);
}
[self setNeedsDisplay];
}
#pragma mark - Size
-(void)setSwitchSize:(CGSize)switchSize{
kSwitchWidth = switchSize.width;
kSwitchHeight = switchSize.height;
CGRect oldFrame = self.frame;
CGRect newFrame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y, kSwitchWidth, kSwitchHeight);
[self setFrame:newFrame];
[self setNeedsDisplay];
}
#pragma mark - Color
-(void)setTintColor:(UIColor *)tintColor{
}
-(void)setThumbTintColor:(UIColor *)thumbTintColor{
_trackLayer.fillColor = [thumbTintColor CGColor];
}
-(void)setOnTintColor:(UIColor *)onTintColor{
onColor = onTintColor;
if (self.stateisOn) {
[self setNeedsDisplay];
}
}
#pragma mark - Public
-(BOOL)isOn{
return self.stateisOn;
}
-(void)setOn:(BOOL)on animated:(BOOL)animated{
shouldAnimated = YES;
self.stateisOn = !on;
[self switchStateChanged];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
CGSize size = CGSizeMake(kSwitchWidth, kSwitchHeight);
CGFloat halfHeight = size.height / 2;
CGFloat width = size.width;
CGFloat height = size.height;
UIBezierPath* pathLeft = [UIBezierPath bezierPathWithArcCenter:CGPointMake(halfHeight + 1, halfHeight)//圆弧中心
radius:halfHeight//半径
startAngle:degreesToRadians(90)//开始角度
endAngle:degreesToRadians(270)//结束角度
clockwise:YES];//是否顺时针
pathLeft.lineWidth = 1.0;
pathLeft.lineCapStyle = kCGLineCapRound; //线条拐角
pathLeft.lineJoinStyle = kCGLineCapRound; //终点处理
[pathLeft addLineToPoint:CGPointMake(width - halfHeight, 0)];
UIBezierPath *pathRight = [UIBezierPath bezierPathWithArcCenter:CGPointMake(width - 1 - halfHeight, halfHeight)
radius:halfHeight
startAngle:degreesToRadians(270)
endAngle:degreesToRadians(90)
clockwise:YES];
pathRight.lineWidth = 1.0;
[pathLeft appendPath:pathRight];
[pathLeft addLineToPoint:CGPointMake(halfHeight, height)];
//
UIColor *fontColor = [UIColor whiteColor];//字体颜色
if (self.stateisOn) {
[onColor setStroke];
[onColor setFill];
fontColor = [UIColor whiteColor];
}
else{
[[UIColor groupTableViewBackgroundColor] setStroke];
[offColor setFill];
fontColor = [UIColor darkGrayColor];
}
[pathLeft fill];
[pathLeft stroke];
UIBezierPath* pathCircle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(halfHeight, halfHeight)//圆弧中心
radius:halfHeight//半径
startAngle:degreesToRadians(0)//开始角度
endAngle:degreesToRadians(360)//结束角度
clockwise:YES];//是否顺时针
pathCircle.lineWidth = 1.0;
pathCircle.lineCapStyle = kCGLineCapRound; //线条拐角
pathCircle.lineJoinStyle = kCGLineCapRound; //终点处理
_trackLayer.path =[pathCircle CGPath];
NSDictionary *attributes =@{
NSFontAttributeName:[UIFont systemFontOfSize:18],
NSForegroundColorAttributeName:fontColor
};
if (self.onText && self.onText.length > 0) {
CGSize size = [self.onText sizeWithAttributes:attributes];
[self.onText drawInRect:CGRectMake((width/2 - size.width)/2, (height-size.height) / 2, width / 2, height) withAttributes:attributes];
}
if (self.offText && self.offText.length > 0) {
CGSize size = [self.onText sizeWithAttributes:attributes];
[self.offText drawInRect:CGRectMake(width/2 + (width/2 - size.width)/2, (height-size.height) / 2, width / 2, height) withAttributes:attributes];
}
}
@end