-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathXXXLabeledSliderCell.m
208 lines (164 loc) · 9.32 KB
/
XXXLabeledSliderCell.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
#import "XXXLabeledSliderCell.h"
@implementation XXXLabeledSliderCell {
UIStackView *_stackView;
UIStackView *_sliderStackView;
UILabel *_sliderLabel;
UILabel *_valueLabel;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)identifier specifier:(PSSpecifier *)specifier {
self = [super initWithStyle:style reuseIdentifier:identifier specifier:specifier];
if(self) {
[specifier setProperty:@56 forKey:@"height"];
NSBundle *bundle = [specifier.target bundle];
NSString *label = [specifier propertyForKey:@"label"];
NSString *localizationTable = [specifier propertyForKey:@"localizationTable"];
//Create slider label with label property of specifier
_sliderLabel = [[UILabel alloc] init];
_sliderLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightBold];
_sliderLabel.text = [bundle localizedStringForKey:label value:label table:localizationTable];
_sliderLabel.translatesAutoresizingMaskIntoConstraints = NO;
//Create value label
_valueLabel = [[UILabel alloc] init];
_valueLabel.font = [UIFont monospacedDigitSystemFontOfSize:10 weight:UIFontWeightBold];
_valueLabel.text = [NSString stringWithFormat:@"%.01f", [[specifier performGetter] floatValue]];
_valueLabel.userInteractionEnabled = YES;
_valueLabel.translatesAutoresizingMaskIntoConstraints = NO;
[_valueLabel sizeToFit];
_sliderStackView = [[UIStackView alloc] initWithArrangedSubviews:@[self.control, _valueLabel]];
_sliderStackView.alignment = UIStackViewAlignmentCenter;
_sliderStackView.axis = UILayoutConstraintAxisHorizontal;
_sliderStackView.distribution = UIStackViewDistributionFill;
_sliderStackView.spacing = 5;
_sliderStackView.translatesAutoresizingMaskIntoConstraints = NO;
_stackView = [[UIStackView alloc] initWithArrangedSubviews:@[_sliderLabel, _sliderStackView]];
_stackView.alignment = UIStackViewAlignmentCenter;
_stackView.axis = UILayoutConstraintAxisVertical;
_stackView.distribution = UIStackViewDistributionEqualCentering;
_stackView.spacing = 0;
_stackView.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_stackView];
[NSLayoutConstraint activateConstraints:@[
[_stackView.topAnchor constraintEqualToAnchor:self.contentView.topAnchor constant:4],
[_stackView.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:15],
[_stackView.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:-15],
[_stackView.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor constant:-4],
[_sliderStackView.widthAnchor constraintEqualToAnchor:_stackView.widthAnchor],
[_valueLabel.heightAnchor constraintEqualToAnchor:_sliderStackView.heightAnchor],
]];
//Add control events to update value label
[self.control addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventTouchDragInside];
[self.control addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventTouchDragOutside];
[self.control addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
//Add gesture to value label to set custom value
UITapGestureRecognizer *enterCustomValueTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(setCustomSliderValue)];
enterCustomValueTap.numberOfTapsRequired = 1;
[_valueLabel addGestureRecognizer:enterCustomValueTap];
// _infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
// _infoButton.translatesAutoresizingMaskIntoConstraints = NO;
// [_infoButton addTarget:self action:@selector(infoButtonTapped) forControlEvents:UIControlEventTouchUpInside];
// [self.contentView addSubview:_infoButton];
// // Size constraints
// CGFloat buttonSize = 20; // Set the size you want
// [_infoButton addConstraint:[NSLayoutConstraint constraintWithItem:_infoButton
// attribute:NSLayoutAttributeHeight
// relatedBy:NSLayoutRelationEqual
// toItem:nil
// attribute:NSLayoutAttributeNotAnAttribute
// multiplier:1.0
// constant:buttonSize]];
// [_infoButton addConstraint:[NSLayoutConstraint constraintWithItem:_infoButton
// attribute:NSLayoutAttributeWidth
// relatedBy:NSLayoutRelationEqual
// toItem:nil
// attribute:NSLayoutAttributeNotAnAttribute
// multiplier:1.0
// constant:buttonSize]];
// Position constraints
[NSLayoutConstraint activateConstraints:@[
// [_infoButton.centerYAnchor constraintEqualToAnchor:self.contentView.centerYAnchor constant:-14.5],
// [_infoButton.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:-105]
]];
}
return self;
}
//Show alert
-(IBAction)infoButtonTapped {
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *title = ([self.specifier propertyForKey:@"infoTitle"]) ?: [self.specifier propertyForKey:@"label"];
NSString *message = [self.specifier propertyForKey:@"infoMessage"] ?: @"No information provided for this cell.";
NSString *localizedMessage = [bundle localizedStringForKey:message value:message table:[self.specifier propertyForKey:@"localizationTable"]];
UIAlertController *infoAlert = [UIAlertController alertControllerWithTitle:title message:[localizedMessage stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[infoAlert addAction:cancelAction];
UIViewController *rootViewController = self._viewControllerForAncestor;
if (!rootViewController) {
UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
rootViewController = window.rootViewController;
}
[rootViewController presentViewController:infoAlert animated:YES completion:nil];
}
//Present a alert with textfield to let the user enter a custom value
-(void)setCustomSliderValue {
UISlider *slider = (UISlider *)self.control;
NSString *currentValue = [NSString stringWithFormat:@"%.02f", slider.value];
UIAlertController *enterValueAlert = [UIAlertController alertControllerWithTitle:@"Enter Value" message:nil preferredStyle:UIAlertControllerStyleAlert];
[enterValueAlert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.keyboardType = UIKeyboardTypeDecimalPad;
textField.text = currentValue;
textField.textColor = [UIColor orangeColor];
}];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"Set" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
UITextField *textField = enterValueAlert.textFields[0];
CGFloat newValue = [textField.text floatValue];
//Check that value doesnt exceed limits
if(newValue > slider.maximumValue) {
newValue = slider.maximumValue;
} else if(newValue < slider.minimumValue) {
newValue = slider.minimumValue;
}
[self.specifier performSetterWithValue:[NSNumber numberWithFloat:newValue]];
[slider setValue:newValue animated:YES];
[self sliderValueChanged:slider];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[enterValueAlert addAction:confirmAction];
[enterValueAlert addAction:cancelAction];
UIViewController *rootViewController = [self appropriateViewController];
[rootViewController presentViewController:enterValueAlert animated:YES completion:nil];
}
- (UIViewController *)appropriateViewController {
UIViewController *viewController = self._viewControllerForAncestor;
if (!viewController) {
NSArray *windows = [UIApplication sharedApplication].windows;
for (UIWindow *window in windows) {
if (window.isKeyWindow) {
viewController = window.rootViewController;
break;
}
}
}
while (viewController.presentedViewController) {
viewController = viewController.presentedViewController;
}
return viewController;
}
//Update value label when the value changes
-(void)sliderValueChanged:(UISlider *)slider {
_valueLabel.text = [NSString stringWithFormat:@"%.01f", slider.value];
}
//Tint the title and value labels
-(void)tintColorDidChange {
[super tintColorDidChange];
//_infoButton.tintColor = [UIColor orangeColor];
_sliderLabel.textColor = [UIColor orangeColor];
_valueLabel.textColor = [UIColor orangeColor];
}
-(void)refreshCellContentsWithSpecifier:(PSSpecifier *)specifier {
[super refreshCellContentsWithSpecifier:specifier];
if([self respondsToSelector:@selector(tintColor)]) {
_sliderLabel.textColor = [UIColor orangeColor];
_valueLabel.textColor = [UIColor orangeColor];
//_infoButton.tintColor = [UIColor orangeColor];
}
}
@end