-
Notifications
You must be signed in to change notification settings - Fork 31
/
DayNightSwitch.m
408 lines (302 loc) · 13.1 KB
/
DayNightSwitch.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//
// DayNightSwitch.m
// DayNightSwitch
//
// Created by Finn Gaida on 03.09.16.
// Copyright © 2016 Finn Gaida. All rights reserved.
//
#import "DayNightSwitch.h"
/// some color constants
#define onKnobColor [UIColor colorWithRed: 0.882 green: 0.765 blue: 0.325 alpha: 1];
#define onSubviewColor [UIColor colorWithRed: 0.992 green: 0.875 blue: 0.459 alpha: 1];
#define offKnobColor [UIColor colorWithRed: 0.894 green: 0.902 blue: 0.788 alpha: 1];
#define offSubviewColor [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 1];
#define offColor [UIColor colorWithRed: 0.235 green: 0.255 blue: 0.271 alpha: 1];
#define offBorderColor [UIColor colorWithRed: 0.11 green: 0.11 blue: 0.11 alpha: 1];
#define onColor [UIColor colorWithRed: 0.627 green: 0.894 blue: 0.98 alpha: 1];
#define onBorderColor [UIColor colorWithRed: 0.533 green: 0.769 blue: 0.843 alpha: 1];
@interface Knob : UIView
/// Visual state of the knob, animates changes
@property (nonatomic) BOOL on;
/// Horizontally expanded state of the knob, animates changes
@property (nonatomic) BOOL expanded;
/// Round subview of the knob
@property (nonatomic) UIView *subview;
/// Circular subviews on the off state `subview`
@property (nonatomic) NSArray<UIView *> *craters;
@end
@implementation Knob
/// Distance from knob to subview circle
- (CGFloat)subviewMargin {
return self.frame.size.height / 12;
}
/**
Sets up the `subview` with the craters as well
- returns: the view
*/
- (UIView *)setupSubview {
UIView *v = [[UIView alloc] initWithFrame: CGRectMake([self subviewMargin], [self subviewMargin], self.frame.size.width - [self subviewMargin] * 2, self.frame.size.height - [self subviewMargin] * 2)];
v.layer.masksToBounds = true;
v.layer.cornerRadius = v.frame.size.height / 2;
v.backgroundColor = offSubviewColor;
for (UIView *c in [self setupCraters]) {
[v addSubview:c];
}
self.subview = v;
return v;
}
/**
Sets up three craters
- returns: array of set up views
*/
- (NSArray *)setupCraters {
// shortcuts
CGFloat w = self.frame.size.width;
CGFloat h = self.frame.size.height;
UIView *topLeft = [[UIView alloc] initWithFrame: CGRectMake(0, h * 0.1, w * 0.2, w * 0.2)];
UIView *topRight = [[UIView alloc] initWithFrame: CGRectMake(w * 0.5, 0, w * 0.3, w * 0.3)];
UIView *bottom = [[UIView alloc] initWithFrame: CGRectMake(w * 0.4, h * 0.5, w * 0.25, w * 0.25)];
NSArray<UIView *> *all = @[topLeft, topRight, bottom];
for (UIView *v in all) {
v.backgroundColor = offSubviewColor;
v.layer.masksToBounds = YES;
v.layer.cornerRadius = v.frame.size.height / 2;
UIColor *offC = offKnobColor;
v.layer.borderColor = offC.CGColor;
v.layer.borderWidth = [self subviewMargin];
}
self.craters = all;
return all;
}
- (instancetype)initWithFrame:(CGRect)frame {
self.on = NO;
self.expanded = NO;
self = [super initWithFrame:frame];
self.layer.masksToBounds = YES;
self.layer.cornerRadius = self.frame.size.height / 2;
self.backgroundColor = offKnobColor;
[self addSubview:[self setupSubview]];
[self addObserver:self forKeyPath:@"on" options:0 context:nil];
[self addObserver:self forKeyPath:@"expanded" options:0 context:nil];
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"on"]) {
[UIView animateWithDuration:0.8 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
if (self.on) {
self.backgroundColor = onKnobColor;
self.subview.backgroundColor = onSubviewColor;
} else {
self.backgroundColor = offKnobColor;
self.subview.backgroundColor = offSubviewColor;
}
BOOL cache = self.expanded;
self.expanded = cache;
} completion:nil];
[UIView animateWithDuration:0.4 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
self.subview.transform = CGAffineTransformMakeRotation(M_PI * ((self.on) ? 0.2 : -0.2));
} completion: nil];
} else if ([keyPath isEqualToString:@"expanded"]) {
CGFloat newWidth = self.frame.size.height * (self.expanded ? 1.25 : 1);
CGFloat x = (self.on) ? self.superview.frame.size.width - newWidth - [(DayNightSwitch *)self.superview knobMargin] : self.frame.origin.x;
[UIView animateWithDuration:0.8 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
self.frame = CGRectMake(x, self.frame.origin.y, newWidth, self.frame.size.height);
self.subview.center = CGPointMake((self.on) ? self.frame.size.width - self.frame.size.height / 2 : self.frame.size.height / 2, self.subview.center.y);
for (UIView *v in self.craters) {
v.alpha = (self.on) ? 0 : 1;
}
} completion: nil];
}
}
@end
@interface DayNightSwitch ()
/// Round white knob
@property (nonatomic) Knob *knob;
@property (nonatomic) BOOL moved;
/// This prevents the tap gesture recognizer from interfering the drag movement
@property (nonatomic) BOOL dragging;
@end
/// A switch inspired by [Dribbble](https://dribbble.com/shots/1909289-Day-Night-Toggle-Button-GIF)a
@implementation DayNightSwitch
/// Width of the darker border of the background
- (CGFloat)borderWidth {
return self.frame.size.height / 7;
}
/// Distance between border and knob
- (CGFloat)knobMargin {
return self.frame.size.height / 10;
}
/**
Sets up the `knob`
- returns: the knob view
*/
- (Knob *)setupKnob {
CGFloat w = self.frame.size.height - [self knobMargin] * 2;
Knob *v = [[Knob alloc] initWithFrame: CGRectMake([self knobMargin], [self knobMargin], w, w)];
self.knob = v;
return v;
}
/**
Sets up the border layers
- returns: array containing both layers
*/
- (NSArray *)setupBorders {
CAShapeLayer *b1 = [CAShapeLayer layer];
CAShapeLayer *b2 = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) cornerRadius: self.frame.size.height / 2];
b1.path = path.CGPath;
b1.fillColor = [UIColor clearColor].CGColor;
UIColor *onC = onBorderColor;
b1.strokeColor = onC.CGColor;
b1.lineWidth = [self borderWidth];
self.onBorder = b1;
b2.path = path.CGPath;
b2.fillColor = [UIColor clearColor].CGColor;
UIColor *offC = offBorderColor;
b2.strokeColor = offC.CGColor;
b2.lineWidth = [self borderWidth];
self.offBorder = b2;
return @[b1, b2];
}
/**
Creates 7 stars with different location and size
- returns: an array of set up views
*/
- (NSArray *)setupStars {
// shortcuts
CGFloat w = self.frame.size.width;
CGFloat h = self.frame.size.height;
CGFloat x = h * 0.05;
UIView *s1 = [[UIView alloc] initWithFrame: CGRectMake(w * 0.5, h * 0.16, x, x)];
UIView *s2 = [[UIView alloc] initWithFrame: CGRectMake(w * 0.62, h * 0.33, x * 0.6, x * 0.6)];
UIView *s3 = [[UIView alloc] initWithFrame: CGRectMake(w * 0.7, h * 0.15, x, x)];
UIView *s4 = [[UIView alloc] initWithFrame: CGRectMake(w * 0.83, h * 0.39, x * 1.4, x * 1.4)];
UIView *s5 = [[UIView alloc] initWithFrame: CGRectMake(w * 0.7, h * 0.54, x * 0.8, x * 0.8)];
UIView *s6 = [[UIView alloc] initWithFrame: CGRectMake(w * 0.52, h * 0.73, x * 1.3, x * 1.3)];
UIView *s7 = [[UIView alloc] initWithFrame: CGRectMake(w * 0.82, h * 0.66, x * 1.1, x * 1.1)];
NSArray *all = @[s1, s2, s3, s4, s5, s6, s7];
for (UIView *s in all) {
s.layer.masksToBounds = YES;
s.layer.cornerRadius = s.frame.size.height / 2;
s.backgroundColor = [UIColor whiteColor];
}
self.stars = all;
return all;
}
/**
Sets up the `cloud`
- returns: the image view
*/
- (UIImageView *)setupCloud {
UIImageView *v = [[UIImageView alloc] initWithFrame: CGRectMake(self.frame.size.width / 3, self.frame.size.height * 0.4, self.frame.size.width / 3, self.frame.size.width * 0.23)];
v.image = [UIImage imageNamed:@"cloud"];
v.transform = CGAffineTransformMakeScale(0, 0);
// this should be done with UIBezierPaths...
self.cloud = v;
return v;
}
// MARK: handling touch events
- (void)proccessTouches:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (!self.moved) { self.on = !self.on; return; }
CGFloat x = [(UITouch *)touches.allObjects.lastObject locationInView:self].x;
if (x > self.frame.size.width / 2 && !self.on) {
self.on = YES;
} else if (x < self.frame.size.width / 2 && self.on) {
self.on = NO;
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.dragging = YES;
self.knob.expanded = YES;
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.moved = YES;
[self proccessTouches:touches withEvent:event];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self proccessTouches:touches withEvent:event];
self.knob.expanded = NO;
self.dragging = NO;
self.moved = NO;
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self touchesEnded:touches withEvent:event];
}
// MARK: Initializers
- (instancetype)initWithCenter:(CGPoint)center {
CGFloat height = 30;
CGFloat width = height * 1.75;
self = [super initWithFrame: CGRectMake(center.x - width / 2, center.y - height / 2, width, height)];
[self commonInit];
return self;
}
/**
Init method called by all initializers. The switch is initialized off by default
*/
- (void)commonInit {
self.moved = NO;
self.dragging = NO;
self.layer.masksToBounds = YES;
self.layer.cornerRadius = self.frame.size.height / 2;
self.backgroundColor = [UIColor colorWithRed: 0.235 green: 0.255 blue: 0.271 alpha: 1];
NSArray *borders = [self setupBorders];
[self.layer addSublayer:borders[0]];
[self.layer addSublayer:borders[1]];
for (UIView *v in [self setupStars]) {
[self addSubview:v];
}
[self addSubview:[self setupKnob]];
[self addSubview:[self setupCloud]];
[self addObserver:self forKeyPath:@"on" options:0 context:nil];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
[self commonInit];
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self commonInit];
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"on"]) {
// call the action closure
if (self.changeAction) { self.changeAction(self.on); }
self.knob.on = self.on;
[UIView animateWithDuration:0.4 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
CGFloat knobRadius = self.knob.frame.size.width / 2;
if (self.on) {
self.knob.center = CGPointMake(self.frame.size.width - knobRadius - [self knobMargin], self.knob.center.y);
self.backgroundColor = onColor;
self.offBorder.strokeStart = 1.0;
self.cloud.transform = CGAffineTransformIdentity;
} else {
self.knob.center = CGPointMake(knobRadius + [self knobMargin], self.knob.center.y);
self.backgroundColor = offColor;
self.offBorder.strokeEnd = 1.0;
self.cloud.transform = CGAffineTransformMakeScale(0, 0);
}
for (int i = 0; i < self.stars.count; i++) {
UIView *star = self.stars[i];
star.alpha = (self.on) ? 0 : 1;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * i * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
star.transform = CGAffineTransformMakeScale(1.5, 1.5);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.05 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
star.transform = CGAffineTransformIdentity;
});
});
}
} completion:^(BOOL finished) {
// reset the values
if (self.on) {
self.offBorder.strokeStart = 0.0;
self.offBorder.strokeEnd = 0.0;
} else {
self.offBorder.strokeStart = 0.0;
self.offBorder.strokeEnd = 1.0;
}
}];
}
}
@end