Edu Caselles
Committed by Edu Caselles

Adds property to disable animations when drawing the pie chart.

@@ -52,6 +52,10 @@ @@ -52,6 +52,10 @@
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 wirhout animation. Default is YES. **/
  57 +@property (nonatomic) BOOL displayAnimated;
  58 +
55 /** Update chart items. Does not update chart itself. */ 59 /** Update chart items. Does not update chart itself. */
56 - (void)updateChartData:(NSArray *)data; 60 - (void)updateChartData:(NSArray *)data;
57 61
@@ -47,25 +47,34 @@ @@ -47,25 +47,34 @@
47 self = [self initWithFrame:frame]; 47 self = [self initWithFrame:frame];
48 if(self){ 48 if(self){
49 _items = [NSArray arrayWithArray:items]; 49 _items = [NSArray arrayWithArray:items];
50 - _selectedItems = [NSMutableDictionary dictionary]; 50 + [self baseInit];
51 - _outerCircleRadius = CGRectGetWidth(self.bounds) / 2;  
52 - _innerCircleRadius = CGRectGetWidth(self.bounds) / 6;  
53 - _descriptionTextColor = [UIColor whiteColor];  
54 - _descriptionTextFont = [UIFont fontWithName:@"Avenir-Medium" size:18.0];  
55 - _descriptionTextShadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];  
56 - _descriptionTextShadowOffset = CGSizeMake(0, 1);  
57 - _duration = 1.0;  
58 - _shouldHighlightSectorOnTouch = YES;  
59 - _enableMultipleSelection = NO;  
60 - _hideValues = NO;  
61 -  
62 - [super setupDefaultValues];  
63 - [self loadDefault];  
64 } 51 }
65 52
66 return self; 53 return self;
67 } 54 }
68 55
  56 +- (void)awakeFromNib{
  57 + [self baseInit];
  58 +}
  59 +
  60 +- (void)baseInit{
  61 + _selectedItems = [NSMutableDictionary dictionary];
  62 + _outerCircleRadius = CGRectGetWidth(self.bounds) / 2;
  63 + _innerCircleRadius = CGRectGetWidth(self.bounds) / 6;
  64 + _descriptionTextColor = [UIColor whiteColor];
  65 + _descriptionTextFont = [UIFont fontWithName:@"Avenir-Medium" size:18.0];
  66 + _descriptionTextShadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
  67 + _descriptionTextShadowOffset = CGSizeMake(0, 1);
  68 + _duration = 1.0;
  69 + _shouldHighlightSectorOnTouch = YES;
  70 + _enableMultipleSelection = NO;
  71 + _hideValues = NO;
  72 + _displayAnimated = YES;
  73 +
  74 + [super setupDefaultValues];
  75 + [self loadDefault];
  76 +}
  77 +
69 - (void)loadDefault{ 78 - (void)loadDefault{
70 __block CGFloat currentTotal = 0; 79 __block CGFloat currentTotal = 0;
71 CGFloat total = [[self.items valueForKeyPath:@"@sum.value"] floatValue]; 80 CGFloat total = [[self.items valueForKeyPath:@"@sum.value"] floatValue];
@@ -129,6 +138,8 @@ @@ -129,6 +138,8 @@
129 [_contentView addSubview:descriptionLabel]; 138 [_contentView addSubview:descriptionLabel];
130 [_descriptionLabels addObject:descriptionLabel]; 139 [_descriptionLabels addObject:descriptionLabel];
131 } 140 }
  141 +
  142 + [self addAnimationIfNeeded];
132 } 143 }
133 144
134 - (UILabel *)descriptionLabelForItemAtIndex:(NSUInteger)index{ 145 - (UILabel *)descriptionLabelForItemAtIndex:(NSUInteger)index{
@@ -244,14 +255,25 @@ @@ -244,14 +255,25 @@
244 endPercentage:1]; 255 endPercentage:1];
245 256
246 _pieLayer.mask = maskLayer; 257 _pieLayer.mask = maskLayer;
247 - CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 258 +}
248 - animation.duration = _duration; 259 +
249 - animation.fromValue = @0; 260 +- (void)addAnimationIfNeeded{
250 - animation.toValue = @1; 261 + if (_displayAnimated) {
251 - animation.delegate = self; 262 + CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
252 - animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 263 + animation.duration = _duration;
253 - animation.removedOnCompletion = YES; 264 + animation.fromValue = @0;
254 - [maskLayer addAnimation:animation forKey:@"circleAnimation"]; 265 + animation.toValue = @1;
  266 + animation.delegate = self;
  267 + animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  268 + animation.removedOnCompletion = YES;
  269 + [_pieLayer.mask addAnimation:animation forKey:@"circleAnimation"];
  270 + }
  271 + else {
  272 + // Add description labels since no animation is required
  273 + [_descriptionLabels enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  274 + [obj setAlpha:1];
  275 + }];
  276 + }
255 } 277 }
256 278
257 - (void)createArcAnimationForLayer:(CAShapeLayer *)layer 279 - (void)createArcAnimationForLayer:(CAShapeLayer *)layer