-
Notifications
You must be signed in to change notification settings - Fork 3
/
JDMNumericControlObject.m
121 lines (80 loc) · 2.84 KB
/
JDMNumericControlObject.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
//
// JDMNumericControlObject.m
// CIFilter
//
// Created by Justin Madewell on 11/22/15.
// Copyright © 2015 Justin Madewell. All rights reserved.
//
#import "JDMNumericControlObject.h"
@interface JDMNumericControlObject ()
@property UIView *superView;
@property UISlider *slider;
@property NSString *title;
@property CGFloat min;
@property CGFloat max;
@property CGFloat def;
@end
@implementation JDMNumericControlObject
-(id)initWithDelegate:(id<JDMNumericControlObjectDelegate>)delegate inView:(UIView *)view withTitle:(NSString *)title withMin:(CGFloat)min andMax:(CGFloat)max withDefault:(CGFloat)defaultValue andKeyValue:(NSString *)keyValue
{
self = [super init];
if(self)
{
self.delegate = delegate;
self.superView = view;
self.min = min;
self.max = max;
self.def = defaultValue;
self.title = title;
self.keyValue = keyValue;
[self setup];
}
return self;
}
-(void)updateYPosition:(CGFloat)positionY
{
// update the views colors
[UIView animateWithDuration:0 animations:^{
self.slider.center = CGPointMake(self.superView.center.x, positionY);
}];
}
-(void)setup
{
self.slider = [self makeSlider];
self.slider.center = CGPointMake(self.superView.center.x, 20);
[self.superView addSubview:self.slider];
}
-(UISlider*)makeSlider
{
CGFloat sliderFrameH = 30;
CGFloat sliderFrameW = self.superView.frame.size.width * 0.75;
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0,0, sliderFrameW, sliderFrameH)];
[slider addTarget:self action:@selector(handleSlider:) forControlEvents:UIControlEventAllTouchEvents];
slider.minimumTrackTintColor = [UIColor blackColor];
slider.minimumValue = self.min;
slider.maximumValue = self.max;
slider.value = self.def;
CGRect labelRect = CGRectMake(0, -10, sliderFrameW, 13);
UILabel *sliderTitle = [[UILabel alloc]initWithFrame:labelRect];
sliderTitle.textAlignment = NSTextAlignmentCenter;
sliderTitle.font = [UIFont fontWithName:@"Helvetica" size:12];
sliderTitle.text = self.title;
[slider addSubview:sliderTitle];
UILabel *valueLabel = [[UILabel alloc]initWithFrame:labelRect];
valueLabel.textAlignment = NSTextAlignmentRight;
valueLabel.font = [UIFont fontWithName:@"Helvetica" size:12];
valueLabel.text = [@"" stringByAppendingFormat:@"%.02f",slider.value];
[slider addSubview:valueLabel];
return slider;
}
-(void)handleSlider:(UISlider*)slider
{
UILabel *label = (UILabel*)[slider.subviews objectAtIndex:1];
label.text = [@"" stringByAppendingFormat:@"%.02f",slider.value];
CGFloat newValue = slider.value;
[self.delegate didChangeNumericValueTo:@(newValue) forKeyValue:self.keyValue];
}
-(void)cleanUpAndRemove
{
}
@end