-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
KAProgressLabel.m
403 lines (336 loc) · 12.5 KB
/
KAProgressLabel.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
//
// KAProgressLabel.m
// KAProgressLabel
//
// Created by Alex on 09/06/13.
// Copyright (c) 2013 Alexis Creuzot. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "KAProgressLabel.h"
#define KADegreesToRadians(degrees) ((degrees)/180.0*M_PI)
#define KARadiansToDegrees(radians) ((radians)*180.0/M_PI)
@implementation KAProgressLabel {
TPPropertyAnimation *_currentStartDegreeAnimation;
TPPropertyAnimation *_currentEndDegreeAnimation;
}
@synthesize startDegree = _startDegree;
@synthesize endDegree = _endDegree;
@synthesize progress = _progress;
#pragma mark Core
-(void)dealloc
{
// KVO
[self removeObserver:self forKeyPath:@"trackWidth"];
[self removeObserver:self forKeyPath:@"progressWidth"];
[self removeObserver:self forKeyPath:@"fillColor"];
[self removeObserver:self forKeyPath:@"trackColor"];
[self removeObserver:self forKeyPath:@"progressColor"];
[self removeObserver:self forKeyPath:@"startDegree"];
[self removeObserver:self forKeyPath:@"endDegree"];
[self removeObserver:self forKeyPath:@"roundedCornersWidth"];
[self.startLabel removeObserver:self forKeyPath:@"text"];
[self.endLabel removeObserver:self forKeyPath:@"text"];
}
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self baseInit];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self) {
[self baseInit];
}
return self;
}
-(void)baseInit
{
// Logic
_startDegree = 0;
_endDegree = 0;
_progress = 0;
// We need a square view
// For now, we resize and center the view
if(self.frame.size.width != self.frame.size.height){
CGRect frame = self.frame;
float delta = ABS(self.frame.size.width-self.frame.size.height)/2;
if(self.frame.size.width > self.frame.size.height){
frame.origin.x += delta;
frame.size.width = self.frame.size.height;
self.frame = frame;
}else{
frame.origin.y += delta;
frame.size.height = self.frame.size.width;
self.frame = frame;
}
}
[self setUserInteractionEnabled:YES];
// Style
self.textAlignment = NSTextAlignmentCenter;
_trackWidth = 5.0;
_progressWidth = 5.0;
_roundedCornersWidth = 0.0;
_fillColor = [UIColor clearColor];
_trackColor = [UIColor lightGrayColor];
_progressColor = [UIColor blackColor];
_startLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_startLabel.textAlignment = NSTextAlignmentCenter;
_startLabel.adjustsFontSizeToFitWidth = YES;
_startLabel.minimumScaleFactor = .1;
_startLabel.clipsToBounds = YES;
_endLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_endLabel.textAlignment = NSTextAlignmentCenter;
_endLabel.adjustsFontSizeToFitWidth = YES;
_endLabel.minimumScaleFactor = .1;
_endLabel.clipsToBounds = YES;
[self addSubview:self.startLabel];
[self addSubview:self.endLabel];
// KVO
[self addObserver:self forKeyPath:@"trackWidth" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"progressWidth" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"fillColor" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"trackColor" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"progressColor" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"startDegree" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"endDegree" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"roundedCornersWidth" options:NSKeyValueObservingOptionNew context:nil];
[_startLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
[_endLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
}
-(void)drawRect:(CGRect)rect
{
[self drawProgressLabelCircleInRect:self.bounds];
[super drawTextInRect:self.bounds];
}
#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
[self setNeedsDisplay] ;
if([keyPath isEqualToString:@"startDegree"] ||
[keyPath isEqualToString:@"endDegree"]){
KAProgressLabel *__unsafe_unretained weakSelf = self;
if(self.labelVCBlock) {
self.labelVCBlock(weakSelf);
}
}
}
#pragma mark - Getters
- (float) radius
{
return MIN(self.frame.size.width,self.frame.size.height)/2;
}
- (CGFloat)startDegree
{
return _startDegree +90;
}
- (CGFloat)endDegree
{
return _endDegree +90;
}
- (CGFloat)progress
{
return self.endDegree/360;
}
#pragma mark - Setters
-(void)setStartDegree:(CGFloat)startDegree
{
_startDegree = startDegree - 90;
}
-(void)setEndDegree:(CGFloat)endDegree
{
_endDegree = endDegree - 90;
}
-(void)setProgress:(CGFloat)progress
{
[self setStartDegree:0];
[self setEndDegree:progress*360];
}
#pragma mark - Animations
-(void)setStartDegree:(CGFloat)startDegree timing:(TPPropertyAnimationTiming)timing duration:(CGFloat)duration delay:(CGFloat)delay
{
TPPropertyAnimation *animation = [TPPropertyAnimation propertyAnimationWithKeyPath:@"startDegree"];
animation.delegate = self;
animation.fromValue = @(_startDegree+90);
animation.toValue = @(startDegree);
animation.duration = duration;
animation.startDelay = delay;
animation.timing = timing;
[animation beginWithTarget:self];
_currentStartDegreeAnimation = animation;
}
-(void)setEndDegree:(CGFloat)endDegree timing:(TPPropertyAnimationTiming)timing duration:(CGFloat)duration delay:(CGFloat)delay
{
TPPropertyAnimation *animation = [TPPropertyAnimation propertyAnimationWithKeyPath:@"endDegree"];
animation.delegate = self;
animation.fromValue = @(_endDegree+90);
animation.toValue = @(endDegree);
animation.duration = duration;
animation.startDelay = delay;
animation.timing = timing;
[animation beginWithTarget:self];
_currentEndDegreeAnimation = animation;
}
-(void)setProgress:(CGFloat)progress timing:(TPPropertyAnimationTiming)timing duration:(CGFloat)duration delay:(CGFloat)delay
{
[self setStartDegree:0];
[self setEndDegree:(progress*360) timing:timing duration:duration delay:delay];
}
- (void) stopAnimations
{
if (_currentStartDegreeAnimation != nil) {
[_currentStartDegreeAnimation cancel];
_currentStartDegreeAnimation = nil;
}
if (_currentEndDegreeAnimation != nil) {
[_currentEndDegreeAnimation cancel];
_currentEndDegreeAnimation = nil;
}
}
- (void)propertyAnimationDidFinish:(TPPropertyAnimation*)propertyAnimation
{
_currentStartDegreeAnimation = nil;
_currentEndDegreeAnimation = nil;
if (self.labelAnimCompleteBlock) {
self.labelAnimCompleteBlock(self);
}
}
#pragma mark - Touch Interaction
// Limit touch to actual disc surface
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if (!self.isUserInteractionEnabled || self.isHidden || self.alpha <= 0.01) {
return nil;
}
UIBezierPath *p = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
return ([p containsPoint:point])? self : nil;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self moveBasedOnTouches:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
[self moveBasedOnTouches:touches withEvent:event];
}
- (void)moveBasedOnTouches:(NSSet *)touches withEvent:(UIEvent *)event
{
// No interaction enabled
if(!self.isStartDegreeUserInteractive &&
!self.isEndDegreeUserInteractive){
return;
}
UITouch * touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self];
// Coordinates to polar
float x = touchLocation.x - self.frame.size.width/2;
float y = touchLocation.y - self.frame.size.height/2;
int angle = KARadiansToDegrees(atan(y/x));
angle += (x>=0)? 90 : 270;
// Interact
if(!self.isStartDegreeUserInteractive) // Only End
{
[self setEndDegree:angle];
}
else if(!self.isEndDegreeUserInteractive) // Only Start
{
[self setStartDegree:angle];
}
else // All,hence move nearest knob
{
float startDelta = sqrt(pow(self.startLabel.center.x-touchLocation.x,2) + pow(self.startLabel.center.y- touchLocation.y,2));
float endDelta = sqrt(pow(self.endLabel.center.x-touchLocation.x,2) + pow(self.endLabel.center.y - touchLocation.y,2));
if(startDelta<endDelta){
[self setStartDegree:angle];
}else{
[self setEndDegree:angle];
}
}
}
#pragma mark - Drawing
-(void)drawProgressLabelCircleInRect:(CGRect)rect
{
CGRect circleRect= [self rectForCircle:rect];
CGFloat archXPos = rect.size.width/2 + rect.origin.x;
CGFloat archYPos = rect.size.height/2 + rect.origin.y;
CGFloat archRadius = (circleRect.size.width) / 2.0;
if(isnan(_endDegree)){
self.endDegree = 0;
}
if(isnan(_startDegree)){
self.startDegree = 0;
}
int clockwise = 0;
if (self.progress < 0.0f) {
clockwise = 1;
}
CGFloat trackStartAngle = KADegreesToRadians(0);
CGFloat trackEndAngle = KADegreesToRadians(360);
CGFloat progressStartAngle = KADegreesToRadians(_startDegree);
CGFloat progressEndAngle = KADegreesToRadians(_endDegree);
CGContextRef context = UIGraphicsGetCurrentContext();
// Circle
CGContextSetFillColorWithColor(context, self.fillColor.CGColor);
CGContextFillEllipseInRect(context, circleRect);
CGContextStrokePath(context);
// Track
CGContextSetStrokeColorWithColor(context, self.trackColor.CGColor);
CGContextSetLineWidth(context, _trackWidth);
CGContextAddArc(context, archXPos,archYPos, archRadius, trackStartAngle, trackEndAngle, 1);
CGContextStrokePath(context);
// Progress
CGContextSetStrokeColorWithColor(context, self.progressColor.CGColor);
CGContextSetLineWidth(context, _progressWidth);
CGContextAddArc(context, archXPos, archYPos, archRadius, progressStartAngle, progressEndAngle, clockwise);
CGContextStrokePath(context);
// Rounded corners
if (_roundedCornersWidth > 0 && self.progress != 0.0f) {
CGContextSetFillColorWithColor(context, self.progressColor.CGColor);
CGContextAddEllipseInRect(context, [self rectForDegree:_startDegree andRect:rect]);
CGContextAddEllipseInRect(context, [self rectForDegree:_endDegree andRect:rect]);
CGContextFillPath(context);
}
self.startLabel.frame = [self rectForDegree:_startDegree andRect:rect];
self.endLabel.frame = [self rectForDegree:_endDegree andRect:rect];
self.startLabel.layer.cornerRadius = [self borderDelta];
self.endLabel.layer.cornerRadius = [self borderDelta];
}
#pragma mark - Helpers
- (CGRect) rectForDegree:(float) degree andRect:(CGRect) rect
{
float x = [self xPosRoundForAngle:degree andRect:rect] - _roundedCornersWidth/2;
float y = [self yPosRoundForAngle:degree andRect:rect] - _roundedCornersWidth/2;
return CGRectMake(x, y, _roundedCornersWidth, _roundedCornersWidth);
}
- (float) xPosRoundForAngle:(float) degree andRect:(CGRect) rect
{
return cosf(KADegreesToRadians(degree))* [self radius]
- cosf(KADegreesToRadians(degree)) * [self borderDelta]
+ rect.size.width/2;
}
- (float) yPosRoundForAngle:(float) degree andRect:(CGRect) rect
{
return sinf(KADegreesToRadians(degree))* [self radius]
- sinf(KADegreesToRadians(degree)) * [self borderDelta]
+ rect.size.height/2;
}
- (float) borderDelta
{
return MAX(MAX(_trackWidth,_progressWidth),_roundedCornersWidth)/2;
}
-(CGRect)rectForCircle:(CGRect)rect
{
CGFloat minDim = MIN(self.bounds.size.width, self.bounds.size.height);
CGFloat circleRadius = (minDim / 2) - [self borderDelta];
CGPoint circleCenter = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
return CGRectMake(circleCenter.x - circleRadius, circleCenter.y - circleRadius, 2 * circleRadius, 2 * circleRadius);
}
@end