-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUUVerticalSliderView.m
More file actions
299 lines (243 loc) · 7.93 KB
/
UUVerticalSliderView.m
File metadata and controls
299 lines (243 loc) · 7.93 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//
// UUVerticalSliderView.h
// Useful Utilities - Vertical slider view drop in replacement for OS horizontal slider
//
// (c) 2014, Jonathan Hays. All Rights Reserved.
//
// Smile License:
// You are free to use this code for whatever purposes you desire. The only requirement is that you smile everytime you use it.
//
// Contact: @cheesemaker or jon@silverpinesoftware.com
#import "UUVerticalSliderView.h"
#ifndef UU_MEMORY_MANAGEMENT
#if !__has_feature(objc_arc)
#define UU_AUTORELEASE(x) [(x) autorelease]
#define UU_RELEASE(x) [(x) release]
#else
#define UU_AUTORELEASE(x) x
#define UU_RELEASE(x) (void)(0)
#endif
#endif
@interface UUVerticalSliderView()
@property (nonatomic, strong) UIImageView* background;
@property (nonatomic, strong) UIImageView* sliderImage;
@property (nonatomic, strong) UILongPressGestureRecognizer* gestureRecognizer;
@end
@implementation UUVerticalSliderView
@synthesize minimumValue = _minimumValue;
@synthesize maximumValue = _maximumValue;
+ (UIImage*) defaultSliderImage
{
CGFloat borderWidth = 2;
UIColor* color = [UIColor blueColor];
UIColor* borderColor = [UIColor colorWithWhite:0.98 alpha:0.9];
CGRect rect = CGRectMake(0, 0, 40, 40);
UIView* view = [[UIView alloc] initWithFrame:rect];
view.backgroundColor = color;
view.layer.borderColor = [borderColor CGColor];
view.layer.cornerRadius = rect.size.height / 2.0;
view.layer.masksToBounds = YES;
view.layer.borderWidth = borderWidth;
view.layer.contentsScale = [[UIScreen mainScreen] scale];
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [[UIScreen mainScreen] scale]);
CGContextRef outputContext = UIGraphicsGetCurrentContext();
[view.layer renderInContext:outputContext];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (instancetype) initWithBackground:(UIImage*)background andSlider:(UIImage*)slider
{
self = [super initWithFrame:CGRectMake(0, 0, background.size.width, background.size.height + (slider.size.height / 2.0))];
if (self)
{
self.backgroundColor = [UIColor clearColor];
self.background = UU_AUTORELEASE([[UIImageView alloc] initWithImage:background]);
[self addSubview:self.background];
self.userInteractionEnabled = YES;
self.sliderImage = UU_AUTORELEASE([[UIImageView alloc] initWithImage:slider]);
[self addSubview:self.sliderImage];
self.sliderImage.center = self.center;
_minimumValue = 0.0;
_maximumValue = 100.0;
[self updateSliders];
self.gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(minimumLongPress:)];
self.gestureRecognizer.delaysTouchesBegan = NO;
self.gestureRecognizer.cancelsTouchesInView = YES;
self.gestureRecognizer.minimumPressDuration = 0.0;
[self.sliderImage addGestureRecognizer:self.gestureRecognizer];
self.sliderImage.userInteractionEnabled = YES;
UU_RELEASE(gestureRecognizer);
}
return self;
}
- (instancetype) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = [UIColor clearColor];
self.userInteractionEnabled = YES;
UIImage* slider = [UUVerticalSliderView defaultSliderImage];
self.sliderImage = UU_AUTORELEASE([[UIImageView alloc] initWithImage:slider]);
[self addSubview:self.sliderImage];
frame.origin.y -= (slider.size.height / 2.0);
frame.size.height += (slider.size.height);
self.frame = frame;
_minimumValue = 0.0;
_maximumValue = 100.0;
[self updateSliders];
UILongPressGestureRecognizer* gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(minimumLongPress:)];
gestureRecognizer.delaysTouchesBegan = NO;
gestureRecognizer.cancelsTouchesInView = YES;
gestureRecognizer.minimumPressDuration = 0.0;
[self.sliderImage addGestureRecognizer:gestureRecognizer];
self.sliderImage.userInteractionEnabled = YES;
UU_RELEASE(gestureRecognizer);
}
return self;
}
- (float) minimumValue
{
return _minimumValue;
}
- (float) maximumValue
{
return _maximumValue;
}
- (void) setMinimumValue:(float)min
{
_minimumValue = min;
[self updateSliders];
}
- (void) setMaximumValue:(float)max
{
_maximumValue = max;
[self updateSliders];
}
- (void) setValue:(float)val
{
[self setSliderValue:val];
}
- (void) setThumbImage:(UIImage *)image forState:(UIControlState)state
{
self.sliderImage.image = image;
}
- (void) setTrackImage:(UIImage *)image forState:(UIControlState)state
{
if (!self.background)
{
self.background = UU_AUTORELEASE([[UIImageView alloc] initWithImage:image]);
self.background.frame = self.bounds;
[self addSubview:self.background];
[self bringSubviewToFront:self.sliderImage];
}
else
{
self.background.image = image;
}
}
- (float) sliderHeight
{
return self.frame.size.height - self.sliderImage.frame.size.height;
}
- (float) sliderMinPosition
{
return (self.sliderImage.frame.size.height / 2.0);
}
- (float) sliderMaxPosition
{
return self.frame.size.height - (self.sliderImage.frame.size.height / 2.0);
}
- (float) valueForPosition:(float)position
{
position -= [self sliderMinPosition];
float percentage = position / [self sliderHeight];
float range = self.maximumValue - self.minimumValue;
float value = self.minimumValue + (percentage * range);
return value;
}
- (float) positionForValue:(float)value
{
float offset = (value - _minimumValue) / (_maximumValue - _minimumValue);
float height = [self sliderHeight];
float position = offset * height;
return position + [self sliderMinPosition];
}
- (void) updateSliders
{
float topPosition = [self positionForValue:self.value];
if (topPosition < [self sliderMinPosition])
{
topPosition = [self sliderMinPosition];
}
if (topPosition > [self sliderMaxPosition])
{
topPosition = [self sliderMaxPosition];
}
CGPoint center = CGPointMake(self.frame.size.width / 2.0, topPosition);
self.sliderImage.center = center;
}
- (void) setSliderValue:(float)min
{
_value = min;
if (self.snapToIntegerValues)
{
_value = (int)(_value + 0.5);
}
if (self.value < self.minimumValue)
self.value = self.minimumValue;
if (self.value > self.maximumValue)
self.value = self.maximumValue;
[self updateSliders];
NSArray* allTargets = [[self allTargets] allObjects];
for (id target in allTargets)
{
NSArray* actions = [self actionsForTarget:target forControlEvent:UIControlEventValueChanged];
for (NSString* actionName in actions)
{
SEL selector = NSSelectorFromString(actionName);
//Since we KNOW this doesn't cause a leak, we suppress the warning
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[target performSelector:selector withObject:self];
#pragma clang diagnostic pop
}
}
}
- (void) minimumLongPress:(UILongPressGestureRecognizer*)gesture
{
CGPoint location = [gesture locationInView:self];
CGPoint current = self.sliderImage.center;
current.y = location.y;
if (current.y < 0)
current.y = 0;
if (current.y > self.frame.size.height)
current.y = self.frame.size.height;
self.sliderImage.center = current;
float value = [self valueForPosition:current.y];
[self setSliderValue:value];
if (gesture.state == UIGestureRecognizerStateBegan)
{
NSArray* allTargets = [[self allTargets] allObjects];
for (id target in allTargets)
{
NSArray* actions = [self actionsForTarget:target forControlEvent:UIControlEventTouchDown];
for (NSString* actionName in actions)
{
SEL selector = NSSelectorFromString(actionName);
//Since we KNOW this doesn't cause a leak, we suppress the warning
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[target performSelector:selector withObject:self];
#pragma clang diagnostic pop
}
}
}
}
- (void) setEnabled:(BOOL)enabled
{
self.gestureRecognizer.enabled = enabled;
}
@end