Kevin

Merge pull request #55 from kyleclegg/master

Made counting label publicly accessible. Adding ability to grow circle.
@@ -8,21 +8,29 @@ @@ -8,21 +8,29 @@
8 8
9 #import <UIKit/UIKit.h> 9 #import <UIKit/UIKit.h>
10 #import "PNColor.h" 10 #import "PNColor.h"
  11 +#import "UICountingLabel.h"
11 12
  13 +typedef NS_ENUM(NSUInteger, PNChartFormatType) {
  14 + PNChartFormatTypePercent,
  15 + PNChartFormatTypeDollar,
  16 + PNChartFormatTypeNone
  17 +};
12 18
13 #define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI) 19 #define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
14 20
15 @interface PNCircleChart : UIView 21 @interface PNCircleChart : UIView
16 22
17 - (void)strokeChart; 23 - (void)strokeChart;
  24 +- (void)growChartByAmount:(NSNumber *)growAmount;
18 - (id)initWithFrame:(CGRect)frame andTotal:(NSNumber *)total andCurrent:(NSNumber *)current andClockwise:(BOOL)clockwise andShadow:(BOOL)hasBackgroundShadow; 25 - (id)initWithFrame:(CGRect)frame andTotal:(NSNumber *)total andCurrent:(NSNumber *)current andClockwise:(BOOL)clockwise andShadow:(BOOL)hasBackgroundShadow;
19 26
  27 +@property (strong, nonatomic) UICountingLabel *countingLabel;
20 @property (nonatomic) UIColor *strokeColor; 28 @property (nonatomic) UIColor *strokeColor;
21 -@property (nonatomic) UIColor *labelColor;  
22 @property (nonatomic) NSNumber *total; 29 @property (nonatomic) NSNumber *total;
23 @property (nonatomic) NSNumber *current; 30 @property (nonatomic) NSNumber *current;
24 @property (nonatomic) NSNumber *lineWidth; 31 @property (nonatomic) NSNumber *lineWidth;
25 -@property (nonatomic) BOOL clockwise; 32 +@property (nonatomic) NSTimeInterval duration;
  33 +@property (nonatomic) PNChartFormatType chartType;
26 34
27 @property (nonatomic) CAShapeLayer *circle; 35 @property (nonatomic) CAShapeLayer *circle;
28 @property (nonatomic) CAShapeLayer *circleBG; 36 @property (nonatomic) CAShapeLayer *circleBG;
@@ -7,25 +7,12 @@ @@ -7,25 +7,12 @@
7 // 7 //
8 8
9 #import "PNCircleChart.h" 9 #import "PNCircleChart.h"
10 -#import "UICountingLabel.h"  
11 -  
12 -@interface PNCircleChart () {  
13 - UICountingLabel *_gradeLabel;  
14 -}  
15 10
  11 +@interface PNCircleChart ()
16 @end 12 @end
17 13
18 @implementation PNCircleChart 14 @implementation PNCircleChart
19 15
20 -- (UIColor *)labelColor  
21 -{  
22 - if (!_labelColor) {  
23 - _labelColor = PNDeepGrey;  
24 - }  
25 -  
26 - return _labelColor;  
27 -}  
28 -  
29 16
30 - (id)initWithFrame:(CGRect)frame andTotal:(NSNumber *)total andCurrent:(NSNumber *)current andClockwise:(BOOL)clockwise andShadow:(BOOL)hasBackgroundShadow 17 - (id)initWithFrame:(CGRect)frame andTotal:(NSNumber *)total andCurrent:(NSNumber *)current andClockwise:(BOOL)clockwise andShadow:(BOOL)hasBackgroundShadow
31 { 18 {
@@ -35,7 +22,8 @@ @@ -35,7 +22,8 @@
35 _total = total; 22 _total = total;
36 _current = current; 23 _current = current;
37 _strokeColor = PNFreshGreen; 24 _strokeColor = PNFreshGreen;
38 - _clockwise = clockwise; 25 + _duration = 1.0;
  26 + _chartType = PNChartFormatTypePercent;
39 27
40 CGFloat startAngle = clockwise ? -90.0f : 270.0f; 28 CGFloat startAngle = clockwise ? -90.0f : 270.0f;
41 CGFloat endAngle = clockwise ? -90.01f : 270.01f; 29 CGFloat endAngle = clockwise ? -90.01f : 270.01f;
@@ -62,7 +50,13 @@ @@ -62,7 +50,13 @@
62 [self.layer addSublayer:_circle]; 50 [self.layer addSublayer:_circle];
63 [self.layer addSublayer:_circleBG]; 51 [self.layer addSublayer:_circleBG];
64 52
65 - _gradeLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(0, 0, 50.0, 50.0)]; 53 + _countingLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(0, 0, 100.0, 50.0)];
  54 + [_countingLabel setTextAlignment:NSTextAlignmentCenter];
  55 + [_countingLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
  56 + [_countingLabel setTextColor:[UIColor grayColor]];
  57 + [_countingLabel setCenter:CGPointMake(self.center.x, self.center.y)];
  58 + _countingLabel.method = UILabelCountingMethodEaseInOut;
  59 + [self addSubview:_countingLabel];;
66 } 60 }
67 61
68 return self; 62 return self;
@@ -71,36 +65,61 @@ @@ -71,36 +65,61 @@
71 65
72 - (void)strokeChart 66 - (void)strokeChart
73 { 67 {
74 - //Add count label 68 + // Add counting label
75 - 69 +
76 - [_gradeLabel setTextAlignment:NSTextAlignmentCenter]; 70 + NSString *format;
77 - [_gradeLabel setFont:[UIFont boldSystemFontOfSize:13.0f]]; 71 + switch (self.chartType) {
78 - [_gradeLabel setTextColor:self.labelColor]; 72 + case PNChartFormatTypePercent:
79 - [_gradeLabel setCenter:CGPointMake(self.center.x, self.center.y)]; 73 + format = @"%d%%";
80 - _gradeLabel.method = UILabelCountingMethodEaseInOut; 74 + break;
81 - _gradeLabel.format = @"%d%%"; 75 + case PNChartFormatTypeDollar:
82 - 76 + format = @"$%d";
  77 + break;
  78 + case PNChartFormatTypeNone:
  79 + default:
  80 + format = @"%d";
  81 + break;
  82 + }
  83 + self.countingLabel.format = format;
  84 + [self addSubview:self.countingLabel];
83 85
84 - [self addSubview:_gradeLabel];  
85 86
86 - //Add circle params 87 + // Add circle params
87 88
88 _circle.lineWidth = [_lineWidth floatValue]; 89 _circle.lineWidth = [_lineWidth floatValue];
89 _circleBG.lineWidth = [_lineWidth floatValue]; 90 _circleBG.lineWidth = [_lineWidth floatValue];
90 _circleBG.strokeEnd = 1.0; 91 _circleBG.strokeEnd = 1.0;
91 _circle.strokeColor = _strokeColor.CGColor; 92 _circle.strokeColor = _strokeColor.CGColor;
92 93
93 - //Add Animation 94 + // Add Animation
94 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 95 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
95 - pathAnimation.duration = 1.0; 96 + pathAnimation.duration = self.duration;
96 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 97 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
97 pathAnimation.fromValue = @0.0f; 98 pathAnimation.fromValue = @0.0f;
98 pathAnimation.toValue = @([_current floatValue] / [_total floatValue]); 99 pathAnimation.toValue = @([_current floatValue] / [_total floatValue]);
99 [_circle addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; 100 [_circle addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
100 _circle.strokeEnd = [_current floatValue] / [_total floatValue]; 101 _circle.strokeEnd = [_current floatValue] / [_total floatValue];
101 102
102 - [_gradeLabel countFrom:0 to:[_current floatValue] / [_total floatValue] * 100 withDuration:1.0]; 103 + [_countingLabel countFrom:0 to:[_current floatValue] withDuration:1.0];
103 } 104 }
104 105
105 106
  107 +
  108 +- (void)growChartByAmount:(NSNumber *)growAmount
  109 +{
  110 + NSNumber *updatedValue = [NSNumber numberWithFloat:[_current floatValue] + [growAmount floatValue]];
  111 +
  112 + // Add animation
  113 + CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
  114 + pathAnimation.duration = self.duration;
  115 + pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  116 + pathAnimation.fromValue = @([_current floatValue] / [_total floatValue]);
  117 + pathAnimation.toValue = @([updatedValue floatValue] / [_total floatValue]);
  118 + _circle.strokeEnd = [updatedValue floatValue] / [_total floatValue];
  119 + [_circle addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
  120 +
  121 + [self.countingLabel countFrom:fmin([_current floatValue], [_total floatValue]) to:fmin([_current floatValue] + [growAmount floatValue], [_total floatValue]) withDuration:self.duration];
  122 + _current = updatedValue;
  123 +}
  124 +
106 @end 125 @end