-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCBDSliderView.m
129 lines (104 loc) · 5.19 KB
/
CBDSliderView.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
#import "CBDSliderView.h"
#import "CBDManager.h"
@implementation CBDSliderView
+ (UIImage *)thumbImage {
static UIImage *blueCircle = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(20.f, 20.f), NO, 0.0f);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGRect rect = CGRectMake(0, 0, 20, 20);
CGContextSetFillColorWithColor(ctx, [UIColor whiteColor].CGColor);
CGContextFillEllipseInRect(ctx, rect);
CGContextRestoreGState(ctx);
blueCircle = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
});
return blueCircle;
}
-(CBDSliderView *)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
self.titleLabel.font = [UIFont boldSystemFontOfSize:14];
self.titleLabel.text = @"SLIDER";
self.titleLabel.textColor = [UIColor whiteColor];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:self.titleLabel];
[NSLayoutConstraint activateConstraints:@[
[self.titleLabel.topAnchor constraintEqualToAnchor:self.topAnchor constant:10],
[self.titleLabel.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
[self.titleLabel.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
[self.titleLabel.heightAnchor constraintEqualToConstant:20]
]];
self.valueButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.valueButton addTarget:self action:@selector(editValue:) forControlEvents:UIControlEventTouchUpInside];
[self.valueButton setTitle:@"0.0" forState:UIControlStateNormal];
self.valueButton.translatesAutoresizingMaskIntoConstraints = NO;
self.valueButton.titleLabel.font = [UIFont systemFontOfSize:14];
self.valueButton.titleLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.valueButton];
[NSLayoutConstraint activateConstraints:@[
[self.valueButton.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-10],
[self.valueButton.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
[self.valueButton.heightAnchor constraintEqualToConstant:35],
[self.valueButton.widthAnchor constraintEqualToConstant:45]
]];
self.slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[self.slider setThumbImage:[CBDSliderView thumbImage] forState:UIControlStateNormal];
[self.slider addTarget:self action:@selector(updateValue:) forControlEvents:UIControlEventValueChanged];
[self.slider addTarget:self action:@selector(touchDrag:) forControlEvents:UIControlEventTouchDragInside];
[self.slider addTarget:self action:@selector(touchUp:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
self.slider.translatesAutoresizingMaskIntoConstraints = NO;
[self.slider setMinimumTrackTintColor:[UIColor whiteColor]];
[self.slider setMaximumTrackTintColor:[UIColor blackColor]];
[self addSubview:self.slider];
[NSLayoutConstraint activateConstraints:@[
[self.slider.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-10],
[self.slider.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:5],
[self.slider.trailingAnchor constraintEqualToAnchor:self.valueButton.leadingAnchor constant:-5],
[self.slider.heightAnchor constraintEqualToConstant:35]
]];
[NSLayoutConstraint activateConstraints:@[
[self.heightAnchor constraintEqualToConstant:70],
]];
return self;
}
-(NSString *)valueAsString {
if (self.isInteger) return [NSString stringWithFormat:@"%.f", self.slider.value];
return [NSString stringWithFormat:@"%.01f", self.slider.value];
}
-(void)updateValue:(id)sender {
[self.valueButton setTitle:[self valueAsString] forState:UIControlStateNormal];
}
-(void)editValue:(id)sender {
NSString *placeholder = [self valueAsString];
UIAlertController *integerInputController = [UIAlertController
alertControllerWithTitle:@"Edit value"
message:@""
preferredStyle:UIAlertControllerStyleAlert];
[integerInputController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = placeholder;
textField.text = placeholder;
}];
[[integerInputController textFields][0] setKeyboardType:UIKeyboardTypeNumberPad];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
if ([integerInputController textFields][0].text) self.slider.value = [[integerInputController textFields][0].text intValue];
[self.slider sendActionsForControlEvents:UIControlEventValueChanged];
}];
[integerInputController addAction:confirmAction];
[[CBDManager sharedInstance] presentViewController:integerInputController animated:YES completion:NULL];
}
-(void)touchDrag:(id)sender {
[UIView animateWithDuration:(0.15) delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[[CBDManager sharedInstance] view].alpha = 0.3;
} completion:nil];
}
-(void)touchUp:(id)sender {
[UIView animateWithDuration:(0.15) delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[[CBDManager sharedInstance] view].alpha = 1.0;
} completion:nil];
}
@end