Committed by
Edu Caselles
Makes animation optional on Bar Chart
Showing
5 changed files
with
39 additions
and
19 deletions
| @@ -31,4 +31,7 @@ | @@ -31,4 +31,7 @@ | ||
| 31 | 31 | ||
| 32 | @property (nonatomic, assign) BOOL isNegative; //!< 是否是负数 | 32 | @property (nonatomic, assign) BOOL isNegative; //!< 是否是负数 |
| 33 | @property (nonatomic, assign) BOOL isShowNumber; //!< 是否显示numbers | 33 | @property (nonatomic, assign) BOOL isShowNumber; //!< 是否显示numbers |
| 34 | + | ||
| 35 | +/** Display the bar with or without animation. Default is YES. **/ | ||
| 36 | +@property (nonatomic) BOOL displayAnimated; | ||
| 34 | @end | 37 | @end |
| @@ -55,6 +55,7 @@ | @@ -55,6 +55,7 @@ | ||
| 55 | 55 | ||
| 56 | [progressline setLineWidth:1.0]; | 56 | [progressline setLineWidth:1.0]; |
| 57 | [progressline setLineCapStyle:kCGLineCapSquare]; | 57 | [progressline setLineCapStyle:kCGLineCapSquare]; |
| 58 | + [self addAnimationIfNeededWithProgressLine:progressline]; | ||
| 58 | 59 | ||
| 59 | 60 | ||
| 60 | if (_barColor) { | 61 | if (_barColor) { |
| @@ -66,20 +67,11 @@ | @@ -66,20 +67,11 @@ | ||
| 66 | 67 | ||
| 67 | if (_grade) { | 68 | if (_grade) { |
| 68 | 69 | ||
| 69 | - CABasicAnimation * pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"]; | ||
| 70 | - pathAnimation.fromValue = (id)_chartLine.path; | ||
| 71 | - pathAnimation.toValue = (id)[progressline CGPath]; | ||
| 72 | - pathAnimation.duration = 0.5f; | ||
| 73 | - pathAnimation.autoreverses = NO; | ||
| 74 | - pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; | ||
| 75 | - [_chartLine addAnimation:pathAnimation forKey:@"animationKey"]; | ||
| 76 | - | ||
| 77 | _chartLine.path = progressline.CGPath; | 70 | _chartLine.path = progressline.CGPath; |
| 78 | 71 | ||
| 79 | if (_barColorGradientStart) { | 72 | if (_barColorGradientStart) { |
| 80 | 73 | ||
| 81 | // Add gradient | 74 | // Add gradient |
| 82 | - [self.gradientMask addAnimation:pathAnimation forKey:@"animationKey"]; | ||
| 83 | self.gradientMask.path = progressline.CGPath; | 75 | self.gradientMask.path = progressline.CGPath; |
| 84 | 76 | ||
| 85 | CABasicAnimation* opacityAnimation = [self fadeAnimation]; | 77 | CABasicAnimation* opacityAnimation = [self fadeAnimation]; |
| @@ -88,13 +80,6 @@ | @@ -88,13 +80,6 @@ | ||
| 88 | } | 80 | } |
| 89 | 81 | ||
| 90 | }else{ | 82 | }else{ |
| 91 | - CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; | ||
| 92 | - pathAnimation.duration = 1.0; | ||
| 93 | - pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; | ||
| 94 | - pathAnimation.fromValue = @0.0f; | ||
| 95 | - pathAnimation.toValue = @1.0f; | ||
| 96 | - [_chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; | ||
| 97 | - | ||
| 98 | _chartLine.strokeEnd = 1.0; | 83 | _chartLine.strokeEnd = 1.0; |
| 99 | 84 | ||
| 100 | _chartLine.path = progressline.CGPath; | 85 | _chartLine.path = progressline.CGPath; |
| @@ -109,7 +94,6 @@ | @@ -109,7 +94,6 @@ | ||
| 109 | self.gradientMask.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height); | 94 | self.gradientMask.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height); |
| 110 | self.gradientMask.path = progressline.CGPath; | 95 | self.gradientMask.path = progressline.CGPath; |
| 111 | 96 | ||
| 112 | - | ||
| 113 | CAGradientLayer *gradientLayer = [CAGradientLayer layer]; | 97 | CAGradientLayer *gradientLayer = [CAGradientLayer layer]; |
| 114 | gradientLayer.startPoint = CGPointMake(0.0,0.0); | 98 | gradientLayer.startPoint = CGPointMake(0.0,0.0); |
| 115 | gradientLayer.endPoint = CGPointMake(1.0 ,0.0); | 99 | gradientLayer.endPoint = CGPointMake(1.0 ,0.0); |
| @@ -127,7 +111,6 @@ | @@ -127,7 +111,6 @@ | ||
| 127 | [_chartLine addSublayer:gradientLayer]; | 111 | [_chartLine addSublayer:gradientLayer]; |
| 128 | 112 | ||
| 129 | self.gradientMask.strokeEnd = 1.0; | 113 | self.gradientMask.strokeEnd = 1.0; |
| 130 | - [self.gradientMask addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; | ||
| 131 | 114 | ||
| 132 | CABasicAnimation* opacityAnimation = [self fadeAnimation]; | 115 | CABasicAnimation* opacityAnimation = [self fadeAnimation]; |
| 133 | [self.textLayer addAnimation:opacityAnimation forKey:nil]; | 116 | [self.textLayer addAnimation:opacityAnimation forKey:nil]; |
| @@ -267,4 +250,31 @@ | @@ -267,4 +250,31 @@ | ||
| 267 | return fadeAnimation; | 250 | return fadeAnimation; |
| 268 | } | 251 | } |
| 269 | 252 | ||
| 253 | +-(void)addAnimationIfNeededWithProgressLine:(UIBezierPath *)progressline | ||
| 254 | +{ | ||
| 255 | + if (_displayAnimated) { | ||
| 256 | + CABasicAnimation *pathAnimation = nil; | ||
| 257 | + | ||
| 258 | + if (_grade) { | ||
| 259 | + pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"]; | ||
| 260 | + pathAnimation.fromValue = (id)_chartLine.path; | ||
| 261 | + pathAnimation.toValue = (id)[progressline CGPath]; | ||
| 262 | + pathAnimation.duration = 0.5f; | ||
| 263 | + pathAnimation.autoreverses = NO; | ||
| 264 | + pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; | ||
| 265 | + [_chartLine addAnimation:pathAnimation forKey:@"animationKey"]; | ||
| 266 | + } | ||
| 267 | + else { | ||
| 268 | + pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; | ||
| 269 | + pathAnimation.duration = 1.0; | ||
| 270 | + pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; | ||
| 271 | + pathAnimation.fromValue = @0.0f; | ||
| 272 | + pathAnimation.toValue = @1.0f; | ||
| 273 | + [_chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; | ||
| 274 | + } | ||
| 275 | + | ||
| 276 | + [self.gradientMask addAnimation:pathAnimation forKey:@"animationKey"]; | ||
| 277 | + } | ||
| 278 | +} | ||
| 279 | + | ||
| 270 | @end | 280 | @end |
| @@ -112,6 +112,9 @@ typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue); | @@ -112,6 +112,9 @@ typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue); | ||
| 112 | /** Controls whether text for x-axis be straight or rotate 45 degree. */ | 112 | /** Controls whether text for x-axis be straight or rotate 45 degree. */ |
| 113 | @property (nonatomic) BOOL rotateForXAxisText; | 113 | @property (nonatomic) BOOL rotateForXAxisText; |
| 114 | 114 | ||
| 115 | +/** Display the bar chart with or without animation. Default is YES. **/ | ||
| 116 | +@property (nonatomic) BOOL displayAnimated; | ||
| 117 | + | ||
| 115 | @property (nonatomic, weak) id<PNChartDelegate> delegate; | 118 | @property (nonatomic, weak) id<PNChartDelegate> delegate; |
| 116 | 119 | ||
| 117 | /**whether show gradient bar*/ | 120 | /**whether show gradient bar*/ |
| @@ -68,6 +68,7 @@ | @@ -68,6 +68,7 @@ | ||
| 68 | _rotateForXAxisText = false; | 68 | _rotateForXAxisText = false; |
| 69 | _isGradientShow = YES; | 69 | _isGradientShow = YES; |
| 70 | _isShowNumbers = YES; | 70 | _isShowNumbers = YES; |
| 71 | + _displayAnimated = YES; | ||
| 71 | _yLabelPrefix = @""; | 72 | _yLabelPrefix = @""; |
| 72 | _yLabelSuffix = @""; | 73 | _yLabelSuffix = @""; |
| 73 | _yLabelFormatter = ^(CGFloat yValue){ | 74 | _yLabelFormatter = ^(CGFloat yValue){ |
| @@ -242,6 +243,9 @@ | @@ -242,6 +243,9 @@ | ||
| 242 | //Change Bar Radius | 243 | //Change Bar Radius |
| 243 | bar.barRadius = _barRadius; | 244 | bar.barRadius = _barRadius; |
| 244 | 245 | ||
| 246 | + //Set Bar Animation | ||
| 247 | + bar.displayAnimated = _displayAnimated; | ||
| 248 | + | ||
| 245 | //Change Bar Background color | 249 | //Change Bar Background color |
| 246 | bar.backgroundColor = _barBackgroundColor; | 250 | bar.backgroundColor = _barBackgroundColor; |
| 247 | //Bar StrokColor First | 251 | //Bar StrokColor First |
| @@ -53,7 +53,7 @@ | @@ -53,7 +53,7 @@ | ||
| 53 | @property (nonatomic, weak) id<PNChartDelegate> delegate; | 53 | @property (nonatomic, weak) id<PNChartDelegate> delegate; |
| 54 | 54 | ||
| 55 | 55 | ||
| 56 | -/** Display the pie chart with or wirhout animation. Default is YES. **/ | 56 | +/** Display the pie chart with or without animation. Default is YES. **/ |
| 57 | @property (nonatomic) BOOL displayAnimated; | 57 | @property (nonatomic) BOOL displayAnimated; |
| 58 | 58 | ||
| 59 | /** Update chart items. Does not update chart itself. */ | 59 | /** Update chart items. Does not update chart itself. */ |
-
Please register or login to post a comment