PNCircleChart.m
6.64 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
//
// PNCircleChart.m
// PNChartDemo
//
// Created by kevinzhow on 13-11-30.
// Copyright (c) 2013年 kevinzhow. All rights reserved.
//
#import "PNCircleChart.h"
@interface PNCircleChart ()
@end
@implementation PNCircleChart
- (id)initWithFrame:(CGRect)frame
total:(NSNumber *)total
current:(NSNumber *)current
clockwise:(BOOL)clockwise
shadow:(BOOL)hasBackgroundShadow
{
self = [super initWithFrame:frame];
if (self) {
_total = total;
_current = current;
_strokeColor = PNFreshGreen;
_duration = 1.0;
_chartType = PNChartFormatTypePercent;
CGFloat startAngle = clockwise ? -90.0f : 270.0f;
CGFloat endAngle = clockwise ? -90.01f : 270.01f;
_lineWidth = @8.0f;
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.center.x, self.center.y)
radius:(self.frame.size.height * 0.5) - [_lineWidth floatValue]
startAngle:DEGREES_TO_RADIANS(startAngle)
endAngle:DEGREES_TO_RADIANS(endAngle)
clockwise:clockwise];
_circle = [CAShapeLayer layer];
_circle.path = circlePath.CGPath;
_circle.lineCap = kCALineCapRound;
_circle.fillColor = [UIColor clearColor].CGColor;
_circle.lineWidth = [_lineWidth floatValue];
_circle.zPosition = 1;
_circleBackground = [CAShapeLayer layer];
_circleBackground.path = circlePath.CGPath;
_circleBackground.lineCap = kCALineCapRound;
_circleBackground.fillColor = [UIColor clearColor].CGColor;
_circleBackground.lineWidth = [_lineWidth floatValue];
_circleBackground.strokeColor = (hasBackgroundShadow ? PNLightYellow.CGColor : [UIColor clearColor].CGColor);
_circleBackground.strokeEnd = 1.0;
_circleBackground.zPosition = -1;
[self.layer addSublayer:_circle];
[self.layer addSublayer:_circleBackground];
_countingLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(0, 0, 100.0, 50.0)];
[_countingLabel setTextAlignment:NSTextAlignmentCenter];
[_countingLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
[_countingLabel setTextColor:[UIColor grayColor]];
[_countingLabel setBackgroundColor:[UIColor clearColor]];
[_countingLabel setCenter:CGPointMake(self.center.x, self.center.y)];
_countingLabel.method = UILabelCountingMethodEaseInOut;
[self addSubview:_countingLabel];;
}
return self;
}
- (void)strokeChart
{
// Add counting label
NSString *format;
switch (self.chartType) {
case PNChartFormatTypePercent:
format = @"%d%%";
break;
case PNChartFormatTypeDollar:
format = @"$%d";
break;
case PNChartFormatTypeNone:
default:
format = @"%d";
break;
}
self.countingLabel.format = format;
[self addSubview:self.countingLabel];
// Add circle params
_circle.lineWidth = [_lineWidth floatValue];
_circleBackground.lineWidth = [_lineWidth floatValue];
_circleBackground.strokeEnd = 1.0;
_circle.strokeColor = _strokeColor.CGColor;
// Add Animation
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = self.duration;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pathAnimation.fromValue = @0.0f;
pathAnimation.toValue = @([_current floatValue] / [_total floatValue]);
[_circle addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
_circle.strokeEnd = [_current floatValue] / [_total floatValue];
[_countingLabel countFrom:0 to:[_current floatValue] withDuration:1.0];
// Check if user wants to add a gradient from the start color to the bar color
if (_strokeColorGradientStart) {
// Add gradient
self.gradientMask = [CAShapeLayer layer];
self.gradientMask.fillColor = [[UIColor clearColor] CGColor];
self.gradientMask.strokeColor = [[UIColor blackColor] CGColor];
self.gradientMask.lineWidth = _circle.lineWidth;
self.gradientMask.lineCap = kCALineCapRound;
CGRect gradientFrame = CGRectMake(0, 0, 2*self.bounds.size.width, 2*self.bounds.size.height);
self.gradientMask.frame = gradientFrame;
self.gradientMask.path = _circle.path;
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.startPoint = CGPointMake(0.5,1.0);
gradientLayer.endPoint = CGPointMake(0.5,0.0);
gradientLayer.frame = gradientFrame;
UIColor *endColor = (_strokeColor ? _strokeColor : [UIColor greenColor]);
NSArray *colors = @[
(id)endColor.CGColor,
(id)_strokeColorGradientStart.CGColor
];
gradientLayer.colors = colors;
[gradientLayer setMask:self.gradientMask];
[_circle addSublayer:gradientLayer];
self.gradientMask.strokeEnd = [_current floatValue] / [_total floatValue];
[self.gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
}
}
- (void)growChartByAmount:(NSNumber *)growAmount
{
NSNumber *updatedValue = [NSNumber numberWithFloat:[_current floatValue] + [growAmount floatValue]];
// Add animation
[self updateChartByCurrent:updatedValue];
}
-(void)updateChartByCurrent:(NSNumber *)current{
// Add animation
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = self.duration;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pathAnimation.fromValue = @([_current floatValue] / [_total floatValue]);
pathAnimation.toValue = @([current floatValue] / [_total floatValue]);
_circle.strokeEnd = [current floatValue] / [_total floatValue];
if (_strokeColorGradientStart) {
self.gradientMask.strokeEnd = _circle.strokeEnd;
[self.gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
}
[_circle addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
[self.countingLabel countFrom:fmin([_current floatValue], [_total floatValue]) to:fmin([current floatValue], [_total floatValue]) withDuration:self.duration];
_current = current;
}
@end