PNCircleChart.m
3.15 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
//
// PNCircleChart.m
// PNChartDemo
//
// Created by kevinzhow on 13-11-30.
// Copyright (c) 2013年 kevinzhow. All rights reserved.
//
#import "PNCircleChart.h"
#import "UICountingLabel.h"
@interface PNCircleChart () {
UICountingLabel *_gradeLabel;
}
@end
@implementation PNCircleChart
- (UIColor *)labelColor
{
if (!_labelColor) {
_labelColor = PNDeepGrey;
}
return _labelColor;
}
- (id)initWithFrame:(CGRect)frame andTotal:(NSNumber *)total andCurrent:(NSNumber *)current
{
self = [super initWithFrame:frame];
if (self) {
_total = total;
_current = current;
_strokeColor = PNFreshGreen;
_lineWidth = [NSNumber numberWithFloat:8.0];
UIBezierPath* circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.center.x,self.center.y) radius:self.frame.size.height*0.5 startAngle:DEGREES_TO_RADIANS(270) endAngle:DEGREES_TO_RADIANS(270.01) clockwise:NO];
_circle = [CAShapeLayer layer];
_circle.path = circlePath.CGPath;
_circle.lineCap = kCALineCapRound;
_circle.fillColor = [UIColor clearColor].CGColor;
_circle.lineWidth = [_lineWidth floatValue];
_circle.zPosition = 1;
_circleBG = [CAShapeLayer layer];
_circleBG.path = circlePath.CGPath;
_circleBG.lineCap = kCALineCapRound;
_circleBG.fillColor = [UIColor clearColor].CGColor;
_circleBG.lineWidth = [_lineWidth floatValue];
_circleBG.strokeColor = PNLightYellow.CGColor;
_circleBG.strokeEnd = 1.0;
_circleBG.zPosition = -1;
[self.layer addSublayer:_circle];
[self.layer addSublayer:_circleBG];
_gradeLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(0, 0, 50.0, 50.0)];
}
return self;
}
-(void)strokeChart
{
//Add count label
[_gradeLabel setTextAlignment:NSTextAlignmentCenter];
[_gradeLabel setFont:[UIFont boldSystemFontOfSize:13.0f]];
[_gradeLabel setTextColor:self.labelColor];
[_gradeLabel setCenter:CGPointMake(self.center.x,self.center.y)];
_gradeLabel.method = UILabelCountingMethodEaseInOut;
_gradeLabel.format = @"%d%%";
[self addSubview:_gradeLabel];
//Add circle params
_circle.lineWidth = [_lineWidth floatValue];
_circleBG.lineWidth = [_lineWidth floatValue];
_circleBG.strokeEnd = 1.0;
_circle.strokeColor = _strokeColor.CGColor;
//Add Animation
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 1.0;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:[_current floatValue]/[_total floatValue]];
[_circle addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
_circle.strokeEnd = [_current floatValue]/[_total floatValue];
[_gradeLabel countFrom:0 to:[_current floatValue]/[_total floatValue]*100 withDuration:1.0];
}
@end