-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCustomToggleSwitch.m
More file actions
57 lines (44 loc) · 1.5 KB
/
CustomToggleSwitch.m
File metadata and controls
57 lines (44 loc) · 1.5 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
//
// CustomToggleSwitch.m
// TypeOver
//
// Created by Owen McGirr on 12/08/2013.
// Copyright (c) 2013 Owen McGirr. All rights reserved.
//
#import "CustomToggleSwitch.h"
@implementation CustomToggleSwitch
#pragma mark - toggle switch methods
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// set switch appearance
[self styleOn:[UIColor blackColor] styleOff:[UIColor grayColor]];
}
return self;
}
- (void)accessibilityElementDidBecomeFocused {
// set switch appearance
[self styleOn:[UIColor blueColor] styleOff:[UIColor cyanColor]];
}
- (void)accessibilityElementDidLoseFocus {
// set switch appearance
[self styleOn:[UIColor blackColor] styleOff:[UIColor grayColor]];
}
#pragma mark - drawing
- (void)styleOn:(UIColor *)onColor styleOff:(UIColor *)offColor {
// draw background image
UIGraphicsBeginImageContext([self bounds].size);
CGContextRef context = UIGraphicsGetCurrentContext();
// on state
CGContextSetFillColorWithColor(context, [onColor CGColor]);
CGContextFillRect(context, CGRectMake(0.0, 0.0, [self bounds].size.width, [self bounds].size.height));
[self setOnImage:UIGraphicsGetImageFromCurrentImageContext()];
// off state
CGContextSetFillColorWithColor(context, [offColor CGColor]);
CGContextFillRect(context, CGRectMake(0.0, 0.0, [self bounds].size.width, [self bounds].size.height));
[self setOffImage:UIGraphicsGetImageFromCurrentImageContext()];
// end graphics session
UIGraphicsEndImageContext();
}
@end