Committed by
Edu Caselles
Makes animation optional on Circle Chart
Showing
3 changed files
with
28 additions
and
15 deletions
| @@ -64,11 +64,13 @@ displayCountingLabel:(BOOL)displayCountingLabel | @@ -64,11 +64,13 @@ displayCountingLabel:(BOOL)displayCountingLabel | ||
| 64 | @property (nonatomic) NSTimeInterval duration; | 64 | @property (nonatomic) NSTimeInterval duration; |
| 65 | @property (nonatomic) PNChartFormatType chartType; | 65 | @property (nonatomic) PNChartFormatType chartType; |
| 66 | 66 | ||
| 67 | - | ||
| 68 | @property (nonatomic) CAShapeLayer *circle; | 67 | @property (nonatomic) CAShapeLayer *circle; |
| 69 | @property (nonatomic) CAShapeLayer *gradientMask; | 68 | @property (nonatomic) CAShapeLayer *gradientMask; |
| 70 | @property (nonatomic) CAShapeLayer *circleBackground; | 69 | @property (nonatomic) CAShapeLayer *circleBackground; |
| 71 | 70 | ||
| 72 | @property (nonatomic) BOOL displayCountingLabel; | 71 | @property (nonatomic) BOOL displayCountingLabel; |
| 73 | 72 | ||
| 73 | +/** Display the circle chart with or without animation. Default is YES. **/ | ||
| 74 | +@property (nonatomic) BOOL displayAnimated; | ||
| 75 | + | ||
| 74 | @end | 76 | @end |
| @@ -69,6 +69,7 @@ displayCountingLabel:(BOOL)displayCountingLabel | @@ -69,6 +69,7 @@ displayCountingLabel:(BOOL)displayCountingLabel | ||
| 69 | _strokeColor = PNFreshGreen; | 69 | _strokeColor = PNFreshGreen; |
| 70 | _duration = 1.0; | 70 | _duration = 1.0; |
| 71 | _chartType = PNChartFormatTypePercent; | 71 | _chartType = PNChartFormatTypePercent; |
| 72 | + _displayAnimated = YES; | ||
| 72 | 73 | ||
| 73 | _displayCountingLabel = displayCountingLabel; | 74 | _displayCountingLabel = displayCountingLabel; |
| 74 | 75 | ||
| @@ -153,19 +154,8 @@ displayCountingLabel:(BOOL)displayCountingLabel | @@ -153,19 +154,8 @@ displayCountingLabel:(BOOL)displayCountingLabel | ||
| 153 | _circleBackground.lineWidth = [_lineWidth floatValue]; | 154 | _circleBackground.lineWidth = [_lineWidth floatValue]; |
| 154 | _circleBackground.strokeEnd = 1.0; | 155 | _circleBackground.strokeEnd = 1.0; |
| 155 | _circle.strokeColor = _strokeColor.CGColor; | 156 | _circle.strokeColor = _strokeColor.CGColor; |
| 156 | - | ||
| 157 | - // Add Animation | ||
| 158 | - CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; | ||
| 159 | - pathAnimation.duration = self.duration; | ||
| 160 | - pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; | ||
| 161 | - pathAnimation.fromValue = @0.0f; | ||
| 162 | - pathAnimation.toValue = @([_current floatValue] / [_total floatValue]); | ||
| 163 | - [_circle addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; | ||
| 164 | _circle.strokeEnd = [_current floatValue] / [_total floatValue]; | 157 | _circle.strokeEnd = [_current floatValue] / [_total floatValue]; |
| 165 | 158 | ||
| 166 | - [_countingLabel countFrom:0 to:[_current floatValue]/([_total floatValue]/100.0) withDuration:self.duration]; | ||
| 167 | - | ||
| 168 | - | ||
| 169 | // Check if user wants to add a gradient from the start color to the bar color | 159 | // Check if user wants to add a gradient from the start color to the bar color |
| 170 | if (_strokeColorGradientStart) { | 160 | if (_strokeColorGradientStart) { |
| 171 | 161 | ||
| @@ -195,9 +185,9 @@ displayCountingLabel:(BOOL)displayCountingLabel | @@ -195,9 +185,9 @@ displayCountingLabel:(BOOL)displayCountingLabel | ||
| 195 | [_circle addSublayer:gradientLayer]; | 185 | [_circle addSublayer:gradientLayer]; |
| 196 | 186 | ||
| 197 | self.gradientMask.strokeEnd = [_current floatValue] / [_total floatValue]; | 187 | self.gradientMask.strokeEnd = [_current floatValue] / [_total floatValue]; |
| 198 | - | ||
| 199 | - [self.gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; | ||
| 200 | } | 188 | } |
| 189 | + | ||
| 190 | + [self addAnimationIfNeeded]; | ||
| 201 | } | 191 | } |
| 202 | 192 | ||
| 203 | 193 | ||
| @@ -241,4 +231,26 @@ displayCountingLabel:(BOOL)displayCountingLabel | @@ -241,4 +231,26 @@ displayCountingLabel:(BOOL)displayCountingLabel | ||
| 241 | _total = total; | 231 | _total = total; |
| 242 | } | 232 | } |
| 243 | 233 | ||
| 234 | +- (void)addAnimationIfNeeded | ||
| 235 | +{ | ||
| 236 | + double percentageValue = [_current floatValue]/([_total floatValue]/100.0); | ||
| 237 | + | ||
| 238 | + if (_displayAnimated) { | ||
| 239 | + CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; | ||
| 240 | + pathAnimation.duration = self.duration; | ||
| 241 | + pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; | ||
| 242 | + pathAnimation.fromValue = @(0.0f); | ||
| 243 | + pathAnimation.toValue = @([_current floatValue] / [_total floatValue]); | ||
| 244 | + [_circle addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; | ||
| 245 | + [_countingLabel countFrom:0 to:percentageValue withDuration:self.duration]; | ||
| 246 | + | ||
| 247 | + if (self.gradientMask && _strokeColorGradientStart) { | ||
| 248 | + [self.gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; | ||
| 249 | + } | ||
| 250 | + } | ||
| 251 | + else { | ||
| 252 | + [_countingLabel countFrom:percentageValue to:percentageValue withDuration:self.duration]; | ||
| 253 | + } | ||
| 254 | +} | ||
| 255 | + | ||
| 244 | @end | 256 | @end |
| @@ -52,7 +52,6 @@ | @@ -52,7 +52,6 @@ | ||
| 52 | 52 | ||
| 53 | @property (nonatomic, weak) id<PNChartDelegate> delegate; | 53 | @property (nonatomic, weak) id<PNChartDelegate> delegate; |
| 54 | 54 | ||
| 55 | - | ||
| 56 | /** Display the pie chart with or without animation. Default is YES. **/ | 55 | /** Display the pie chart with or without animation. Default is YES. **/ |
| 57 | @property (nonatomic) BOOL displayAnimated; | 56 | @property (nonatomic) BOOL displayAnimated; |
| 58 | 57 |
-
Please register or login to post a comment