10 #import "MNEValueTrackingSlider.h" |
10 #import "MNEValueTrackingSlider.h" |
11 |
11 |
12 #pragma mark - |
12 #pragma mark - |
13 #pragma mark Private UIView subclass rendering the popup showing slider value |
13 #pragma mark Private UIView subclass rendering the popup showing slider value |
14 @interface SliderValuePopupView : UIView |
14 @interface SliderValuePopupView : UIView |
15 @property (nonatomic, retain) UIFont *font; |
15 @property (nonatomic, strong) UIFont *font; |
16 @property (nonatomic, copy) NSString *text; |
16 @property (nonatomic, copy) NSString *text; |
17 @property (nonatomic) float arrowOffset; |
17 @property (nonatomic) float arrowOffset; |
18 @end |
18 @end |
19 |
19 |
20 @implementation SliderValuePopupView |
20 @implementation SliderValuePopupView |
21 |
21 |
22 @synthesize font = _font; |
22 @synthesize font = _font; |
23 @synthesize text = _text; |
23 @synthesize text = _text; |
24 @synthesize arrowOffset = _arrowOffset; |
24 @synthesize arrowOffset = _arrowOffset; |
25 |
25 |
26 -(id) initWithFrame:(CGRect) frame { |
26 - (id)initWithFrame:(CGRect)frame { |
27 self = [super initWithFrame:frame]; |
27 self = [super initWithFrame:frame]; |
28 if (self) { |
28 if (self) { |
29 self.font = [UIFont boldSystemFontOfSize:18]; |
29 self.font = [UIFont boldSystemFontOfSize:18]; |
30 } |
30 } |
31 return self; |
31 return self; |
32 } |
32 } |
33 |
33 |
34 -(void) dealloc { |
|
35 self.text = nil; |
|
36 self.font = nil; |
|
37 [super dealloc]; |
|
38 } |
|
39 |
34 |
40 -(void) drawRect:(CGRect) rect { |
35 - (void)drawRect:(CGRect)rect { |
41 // Create the path for the rounded rectangle |
36 // Create the path for the rounded rectangle |
42 CGRect roundedRect = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, floorf(self.bounds.size.height * 0.8)); |
37 CGRect roundedRect = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, floorf(self.bounds.size.height * 0.8)); |
43 UIBezierPath *roundedRectPath = [UIBezierPath bezierPathWithRoundedRect:roundedRect cornerRadius:6.0]; |
38 UIBezierPath *roundedRectPath = [UIBezierPath bezierPathWithRoundedRect:roundedRect cornerRadius:6.0]; |
44 roundedRectPath.lineWidth = 2.0f; |
39 roundedRectPath.lineWidth = 2.0f; |
45 |
40 |
93 |
88 |
94 @synthesize thumbRect, textValue; |
89 @synthesize thumbRect, textValue; |
95 |
90 |
96 #pragma mark Private methods |
91 #pragma mark Private methods |
97 |
92 |
98 -(void) _constructSlider { |
93 - (void)_constructSlider { |
99 valuePopupView = [[SliderValuePopupView alloc] initWithFrame:CGRectZero]; |
94 valuePopupView = [[SliderValuePopupView alloc] initWithFrame:CGRectZero]; |
100 valuePopupView.backgroundColor = [UIColor clearColor]; |
95 valuePopupView.backgroundColor = [UIColor clearColor]; |
101 valuePopupView.alpha = 0.0; |
96 valuePopupView.alpha = 0.0; |
102 [self addSubview:valuePopupView]; |
97 [self addSubview:valuePopupView]; |
103 } |
98 } |
104 |
99 |
105 -(void) _fadePopupViewInAndOut:(BOOL)aFadeIn { |
100 - (void)_fadePopupViewInAndOut:(BOOL)aFadeIn { |
106 [UIView beginAnimations:nil context:NULL]; |
101 [UIView beginAnimations:nil context:NULL]; |
107 [UIView setAnimationDuration:0.25]; |
102 [UIView setAnimationDuration:0.25]; |
108 if (aFadeIn) { |
103 if (aFadeIn) { |
109 valuePopupView.alpha = 1.0; |
104 valuePopupView.alpha = 1.0; |
110 } else { |
105 } else { |
111 valuePopupView.alpha = 0.0; |
106 valuePopupView.alpha = 0.0; |
112 } |
107 } |
113 [UIView commitAnimations]; |
108 [UIView commitAnimations]; |
114 } |
109 } |
115 |
110 |
116 -(void) _positionAndUpdatePopupView { |
111 - (void)_positionAndUpdatePopupView { |
117 CGRect _thumbRect = self.thumbRect; |
112 CGRect _thumbRect = self.thumbRect; |
118 CGRect popupRect = CGRectOffset(_thumbRect, 0, -floorf(_thumbRect.size.height * 1.5)); |
113 CGRect popupRect = CGRectOffset(_thumbRect, 0, -floorf(_thumbRect.size.height * 1.5)); |
119 // (-100, -15) determines the size of the the rect |
114 // (-100, -15) determines the size of the the rect |
120 popupRect = CGRectInset(popupRect, -100, -15); |
115 popupRect = CGRectInset(popupRect, -100, -15); |
121 |
116 |
134 [valuePopupView setNeedsDisplay]; |
129 [valuePopupView setNeedsDisplay]; |
135 } |
130 } |
136 |
131 |
137 #pragma mark Memory management |
132 #pragma mark Memory management |
138 |
133 |
139 -(id) initWithFrame:(CGRect) frame { |
134 - (id)initWithFrame:(CGRect)frame { |
140 self = [super initWithFrame:frame]; |
135 self = [super initWithFrame:frame]; |
141 if (self) { |
136 if (self) { |
142 [self _constructSlider]; |
137 [self _constructSlider]; |
143 } |
138 } |
144 return self; |
139 return self; |
145 } |
140 } |
146 |
141 |
147 -(id) initWithCoder:(NSCoder *)aDecoder { |
142 - (id)initWithCoder:(NSCoder *)aDecoder { |
148 self = [super initWithCoder:aDecoder]; |
143 self = [super initWithCoder:aDecoder]; |
149 if (self) { |
144 if (self) { |
150 [self _constructSlider]; |
145 [self _constructSlider]; |
151 } |
146 } |
152 return self; |
147 return self; |
153 } |
148 } |
154 |
149 |
155 -(void) dealloc { |
|
156 [valuePopupView release]; |
|
157 [textValue release]; |
|
158 [super dealloc]; |
|
159 } |
|
160 |
150 |
161 #pragma mark - |
151 #pragma mark - |
162 #pragma mark UIControl touch event tracking |
152 #pragma mark UIControl touch event tracking |
163 -(BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { |
153 - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { |
164 // Fade in and update the popup view |
154 // Fade in and update the popup view |
165 CGPoint touchPoint = [touch locationInView:self]; |
155 CGPoint touchPoint = [touch locationInView:self]; |
166 // Check if the knob is touched. Only in this case show the popup-view |
156 // Check if the knob is touched. Only in this case show the popup-view |
167 if(CGRectContainsPoint(CGRectInset(self.thumbRect, -14.0, -12.0), touchPoint)) { |
157 if(CGRectContainsPoint(CGRectInset(self.thumbRect, -14.0, -12.0), touchPoint)) { |
168 [self _positionAndUpdatePopupView]; |
158 [self _positionAndUpdatePopupView]; |
169 [self _fadePopupViewInAndOut:YES]; |
159 [self _fadePopupViewInAndOut:YES]; |
170 } |
160 } |
171 return [super beginTrackingWithTouch:touch withEvent:event]; |
161 return [super beginTrackingWithTouch:touch withEvent:event]; |
172 } |
162 } |
173 |
163 |
174 -(BOOL) continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { |
164 - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { |
175 // Update the popup view as slider knob is being moved |
165 // Update the popup view as slider knob is being moved |
176 [self _positionAndUpdatePopupView]; |
166 [self _positionAndUpdatePopupView]; |
177 return [super continueTrackingWithTouch:touch withEvent:event]; |
167 return [super continueTrackingWithTouch:touch withEvent:event]; |
178 } |
168 } |
179 |
169 |
180 -(void) cancelTrackingWithEvent:(UIEvent *)event { |
170 - (void)cancelTrackingWithEvent:(UIEvent *)event { |
181 [super cancelTrackingWithEvent:event]; |
171 [super cancelTrackingWithEvent:event]; |
182 } |
172 } |
183 |
173 |
184 -(void) endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { |
174 - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { |
185 // Fade out the popoup view |
175 // Fade out the popoup view |
186 [self _fadePopupViewInAndOut:NO]; |
176 [self _fadePopupViewInAndOut:NO]; |
187 [super endTrackingWithTouch:touch withEvent:event]; |
177 [super endTrackingWithTouch:touch withEvent:event]; |
188 } |
178 } |
189 |
179 |