-
Notifications
You must be signed in to change notification settings - Fork 1
/
UILoopSlider.m
417 lines (367 loc) · 12.7 KB
/
UILoopSlider.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//
// UILoopSlider.m
// LoopMusic
//
// Created by Johann Gan on 4/9/18.
// Copyright © 2018 Cheng Hann Gan. All rights reserved.
//
#import "UILoopSlider.h"
@implementation UILoopSlider
@synthesize boxHeightMultiplier, preLoopBoxHidden, loopBoxHidden, postLoopBoxHidden, preLoopBoxColor, loopBoxColor, postLoopBoxColor, invalidEndBoxColor, invalidEndBoxWidth, loopingEnabled, looping, loopStart, loopEnd, timeBetweenUpdates, fastMode, intervalThreshold, previousValue, getCurrentTime;
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
[self useDefaultParameters];
[self useDefaultGraphics];
getCurrentTime = ^float (void) { return 0; }; // Default block always returns 0 as the current time. Needs to be non-nil for proper functioning.
[self setThumbImageFromFilename:@"thumb.png" :40]; // Necessary workaround for the weird tracking glitch with the slider thumb.
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
// Form the boxes.
if (!preLoopBox && self.maximumValue > 0)
{
// Attempts to completely encapsulate the thumb in the highlighted region, rather than gauging by the thumb's center.
CGFloat loopStartX = [self loopStartCoord];
CGFloat loopEndX = [self loopEndCoord];
// Initialize the boxes.
preLoopBox = [[UIView alloc] initWithFrame:[self createFrame:[self lowerBound] :loopStartX]];
postLoopBox = [[UIView alloc] initWithFrame:[self createFrame:loopEndX :[self upperBound]]];
loopBox = [[UIView alloc] initWithFrame:[self createFrame:loopStartX :loopEndX]];
invalidEndBox = [[UIView alloc] initWithFrame:[self createFrame:[self upperBound] :[self upperBound]+invalidEndBoxWidth]];
// To enable touches to pass through
preLoopBox.userInteractionEnabled = false;
loopBox.userInteractionEnabled = false;
postLoopBox.userInteractionEnabled = false;
invalidEndBox.userInteractionEnabled = false;
// Aesthetics
preLoopBox.layer.cornerRadius = 3;
loopBox.layer.cornerRadius = 3;
postLoopBox.layer.cornerRadius = 3;
invalidEndBox.layer.cornerRadius = 3;
[self insertSubview:preLoopBox belowSubview:self.subviews.lastObject];
[self insertSubview:postLoopBox belowSubview:self.subviews.lastObject];
[self insertSubview:loopBox belowSubview:self.subviews.lastObject];
[self insertSubview:invalidEndBox belowSubview:self.subviews.lastObject];
[self refreshBoxes];
}
}
- (void)useDefaultParameters
{
// Default values
self.minimumValue = 0;
loopingEnabled = true;
self.looping = false;
timeBetweenUpdates = 0.25;
intervalThreshold = 1.5;
fastMode = false;
previousValue = -1;
}
- (void)useDefaultGraphics
{
boxHeightMultiplier = 1.1;
invalidEndBoxWidth = 3;
// Default to hidden
[self unhighlightLoopRegion];
[self unhighlightNonLoopRegion];
// Default colors
float alpha = 0.25;
self.preLoopBoxColor = [[UIColor redColor] colorWithAlphaComponent:alpha];
self.postLoopBoxColor = [[UIColor blueColor] colorWithAlphaComponent:alpha];
self.loopBoxColor = [[UIColor greenColor] colorWithAlphaComponent:alpha];
self.invalidEndBoxColor = [[UIColor redColor] colorWithAlphaComponent:2*alpha];
}
- (void)setThumbImageFromFilename:(NSString *)imageName :(NSInteger)sideLength
{
CGSize size = CGSizeMake(sideLength, sideLength);
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
[[UIImage imageNamed:imageName] drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self setThumbImage:image forState:UIControlStateNormal];
[self setThumbImage:image forState:UIControlStateSelected];
[self setThumbImage:image forState:UIControlStateHighlighted];
[self refreshBoxes]; // Adjust to the new height.
}
/*!
* Creates a rectangle over a given region.
* @param x0 The region start.
* @param x1 The region end.
* @return The rectangle over the region.
*/
- (CGRect)createFrame:(CGFloat)x0 :(CGFloat)x1
{
CGRect trackRect = [self trackRectForBounds:self.bounds];
CGRect thumbRect = [self thumbRectForBounds:self.bounds trackRect:trackRect value:self.value];
CGFloat sliderHeight = thumbRect.size.width;
CGFloat y0 = thumbRect.origin.y - (boxHeightMultiplier - 1)/2 * sliderHeight;
return CGRectMake(x0, y0, x1-x0, boxHeightMultiplier*sliderHeight);
}
/*!
* Gets the x-coordinate of the lower bound of the slider.
* @return The x-coordinate of the lower bound.
*/
- (CGFloat)lowerBound
{
return self.bounds.origin.x;
}
/*!
* Gets the x-coordinate of the upper bound of the slider.
* @return The x-coordinate of the upper bound.
*/
- (CGFloat)upperBound
{
return self.bounds.origin.x + self.bounds.size.width;
}
/*!
* Gets the width of the slider thumb.
* @return The slider thumb width.
*/
- (CGFloat)thumbWidth
{
double extraWidth = 13 / 3.0; // Intrinsic to the default thumb image ("thumb.png") because of the transparent padding. There are ~6-7 pixels on either side in the original image, and the image is scaled from 120px to 40px.
return self.currentThumbImage.size.width - extraWidth;
}
/*!
* Gets the x-coordinate corresponding to a given time on the slider.
* @param time The slider time value.
* @return The x-coordinate corresponding to the time value.
*/
- (CGFloat)coordinateFromTime:(double)time
{
CGRect trackRect = [self trackRectForBounds:self.bounds];
return trackRect.origin.x + [self thumbWidth]/2 + (trackRect.size.width - [self thumbWidth]) * time / self.maximumValue;
}
/*!
* Calculates the coordinate of the loop start point on the slider.
* @return The x-coordinate of the loop start.
*/
- (CGFloat)loopStartCoord
{
return [self coordinateFromTime:loopStart] - [self thumbWidth]/2;
}
/*!
* Calculates the coordinate of the loop end point on the slider.
* @return The x-coordinate of the loop end.
*/
- (CGFloat)loopEndCoord
{
return [self coordinateFromTime:self.loopEnd] + [self thumbWidth]/2;
}
/*!
* Refreshes the state of the loop boxes.
*/
- (void)refreshBoxes
{
// Refresh the hidden states
preLoopBox.hidden = preLoopBoxHidden;
loopBox.hidden = loopBoxHidden;
postLoopBox.hidden = postLoopBoxHidden;
invalidEndBox.hidden = [self validLoopEnd];
// Refresh the colors
preLoopBox.backgroundColor = preLoopBoxColor;
loopBox.backgroundColor = loopBoxColor;
postLoopBox.backgroundColor = postLoopBoxColor;
invalidEndBox.backgroundColor = invalidEndBoxColor;
// Attempts to completely encapsulate the thumb in the highlighted region, rather than gauging by the thumb's center.
CGFloat loopStartX = [self loopStartCoord];
CGFloat loopEndX = [self loopEndCoord];
[UIView animateWithDuration:0.1 animations:^{
self->preLoopBox.frame = [self createFrame:[self lowerBound] :loopStartX];
self->loopBox.frame = [self createFrame:loopStartX :loopEndX];
self->postLoopBox.frame = [self createFrame:loopEndX :[self upperBound]];
}];
}
/*!
* Refreshes the play slider.
* @param timer The timer that invoked this function.
*/
- (void)refreshSlider:(NSTimer *)timer
{
[self setTime:self.getCurrentTime()];
if (!fastMode && self.loopEnd - self.getCurrentTime() <= intervalThreshold*timeBetweenUpdates)
{
[self switchToFastTimerMode];
// NSLog(@"Activating fast mode.");
}
else if (fastMode && self.loopEnd - self.getCurrentTime() > intervalThreshold*timeBetweenUpdates)
{
[self activateUpdateTimer];
// NSLog(@"Deactivating fast mode.");
}
}
- (void)copySettingsFromSlider:(UILoopSlider *)otherSlider
{
self.loopingEnabled = otherSlider.loopingEnabled;
self.timeBetweenUpdates = otherSlider.timeBetweenUpdates;
self.getCurrentTime = otherSlider.getCurrentTime;
self.minimumValue = otherSlider.minimumValue;
self.maximumValue = otherSlider.maximumValue;
self.loopStart = otherSlider.loopStart;
self.loopEnd = otherSlider->loopEnd; // Want the raw value.
self.value = otherSlider.value;
self.previousValue = otherSlider.previousValue;
self.looping = otherSlider.looping;
}
- (void)activateUpdateTimer
{
[self stopUpdateTimer];
fastMode = false;
updateTimer = [NSTimer scheduledTimerWithTimeInterval:timeBetweenUpdates
target:self
selector:@selector(refreshSlider:)
userInfo:nil
repeats:YES];
}
/*!
* Switches the update timer to a much finer resolution. For when the loop point is near.
*/
- (void)switchToFastTimerMode
{
double fastUpdateInterval = 1e-3; // Millisecond resolution
[self stopUpdateTimer];
fastMode = true;
updateTimer = [NSTimer scheduledTimerWithTimeInterval:fastUpdateInterval
target:self
selector:@selector(refreshSlider:)
userInfo:nil
repeats:YES];
}
- (void)stopUpdateTimer
{
if (updateTimer)
{
[updateTimer invalidate];
updateTimer = nil;
}
}
- (void)highlightPreLoopRegion
{
preLoopBoxHidden = false;
[self refreshBoxes];
}
- (void)highlightLoopRegion
{
loopBoxHidden = false;
[self refreshBoxes];
}
- (void)highlightPostLoopRegion
{
postLoopBoxHidden = false;
[self refreshBoxes];
}
- (void)highlightNonLoopRegion
{
[self highlightPreLoopRegion];
[self highlightPostLoopRegion];
}
- (void)unhighlightPreLoopRegion
{
preLoopBoxHidden = true;
[self refreshBoxes];
}
- (void)unhighlightLoopRegion
{
loopBoxHidden = true;
[self refreshBoxes];
}
- (void)unhighlightPostLoopRegion
{
postLoopBoxHidden = true;
[self refreshBoxes];
}
- (void)unhighlightNonLoopRegion
{
[self unhighlightPreLoopRegion];
[self unhighlightPostLoopRegion];
}
- (void)updateAudioPlayer:(AudioPlayer *)player
{
// Check if the value has actually changed, since the valueChanged event seems to fire on touchUp even when the value didn't actually change. This is to get the "snap to the current playing time" feature if the user holds down the slider, lets it play for a bit, and then let's go without moving.
if (self.value != self.previousValue)
{
[self refreshTime];
player.currentTime = self.value;
player.pauseTime = self.value; // Won't have any effect if not paused, since pauseTime gets reset upon pausing anyway.
}
}
- (void)setMinimumValue:(float)minimumValue
{
[super setMinimumValue:minimumValue];
[self refreshBoxes];
}
- (void)setMaximumValue:(float)maximumValue
{
[super setMaximumValue:maximumValue];
[self refreshBoxes];
}
- (void)setLoopStart:(double)loopStart
{
self->loopStart = MAX(0, loopStart);
self.looping = false; // Need to re-evaluate after the change
[self refreshBoxes];
}
- (void)setLoopEnd:(double)loopEnd
{
self->loopEnd = loopEnd;
[self refreshBoxes];
}
- (double)loopEnd
{
return MIN(self.maximumValue, self->loopEnd);
}
- (bool)validLoopEnd
{
return self->loopEnd <= self.maximumValue;
}
- (void)setLooping:(bool)looping
{
self->looping = looping && [self validLoopEnd];
}
- (void)setTime:(double)time
{
// Start looping.
if (loopingEnabled && !looping && time > loopStart)
self.looping = true;
// Handle the wrap-around if needed.
if (loopingEnabled && looping)
self.value = time >= loopStart ? loopStart + fmod(time-loopStart, self.loopEnd-loopStart) : self.loopEnd - fmod(loopStart-time, self.loopEnd-loopStart);
else
self.value = MAX(self.minimumValue, MIN(time, self.maximumValue));
previousValue = self.value;
}
- (void)forceSetTime:(double)time
{
self.value = MAX(self.minimumValue, MIN(time, self.maximumValue));
if (self.value < loopStart || self.value > self.loopEnd)
self.looping = false;
else
self.looping = true;
previousValue = self.value;
}
- (void)refreshTime
{
[self setTime:self.value];
}
- (void)stop
{
self.value = 0;
self.looping = false;
}
- (void)setupNewTrack:(double)trackEnd :(double)loopStart :(double)loopEnd
{
[self stop];
self.maximumValue = trackEnd;
self.loopStart = loopStart;
self.loopEnd = loopEnd;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end