Edu Caselles

Makes animation optional on Scatter Chart

@@ -81,6 +81,8 @@ @@ -81,6 +81,8 @@
81 81
82 - (void)setupDefaultValues 82 - (void)setupDefaultValues
83 { 83 {
  84 + [super setupDefaultValues];
  85 +
84 // Initialization code 86 // Initialization code
85 self.backgroundColor = [UIColor whiteColor]; 87 self.backgroundColor = [UIColor whiteColor];
86 self.clipsToBounds = YES; 88 self.clipsToBounds = YES;
@@ -231,16 +233,11 @@ @@ -231,16 +233,11 @@
231 { 233 {
232 __block CGFloat yFinilizeValue , xFinilizeValue; 234 __block CGFloat yFinilizeValue , xFinilizeValue;
233 __block CGFloat yValue , xValue; 235 __block CGFloat yValue , xValue;
234 - CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 236 +
235 - pathAnimation.duration = _duration;  
236 - pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  
237 - pathAnimation.fromValue = @(0.0f);  
238 - pathAnimation.toValue = @(1.0f);  
239 - pathAnimation.fillMode = kCAFillModeForwards;  
240 - self.layer.opacity = 1;  
241 -  
242 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 237 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
243 - [NSThread sleepForTimeInterval:1]; 238 + if (self.displayAnimated) {
  239 + [NSThread sleepForTimeInterval:1];
  240 + }
244 // update UI on the main thread 241 // update UI on the main thread
245 dispatch_async(dispatch_get_main_queue(), ^{ 242 dispatch_async(dispatch_get_main_queue(), ^{
246 for (PNScatterChartData *chartData in data) { 243 for (PNScatterChartData *chartData in data) {
@@ -256,13 +253,28 @@ @@ -256,13 +253,28 @@
256 CAShapeLayer *shape = [self drawingPointsForChartData:chartData AndWithX:xFinilizeValue AndWithY:yFinilizeValue]; 253 CAShapeLayer *shape = [self drawingPointsForChartData:chartData AndWithX:xFinilizeValue AndWithY:yFinilizeValue];
257 self.pathLayer = shape ; 254 self.pathLayer = shape ;
258 [self.layer addSublayer:self.pathLayer]; 255 [self.layer addSublayer:self.pathLayer];
259 - [self.pathLayer addAnimation:pathAnimation forKey:@"fade"]; 256 +
  257 + [self addAnimationIfNeeded];
260 } 258 }
261 } 259 }
262 }); 260 });
263 }); 261 });
264 } 262 }
265 263
  264 +- (void)addAnimationIfNeeded{
  265 +
  266 + if (self.displayAnimated) {
  267 + CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
  268 + pathAnimation.duration = _duration;
  269 + pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  270 + pathAnimation.fromValue = @(0.0f);
  271 + pathAnimation.toValue = @(1.0f);
  272 + pathAnimation.fillMode = kCAFillModeForwards;
  273 + self.layer.opacity = 1;
  274 + [self.pathLayer addAnimation:pathAnimation forKey:@"fade"];
  275 + }
  276 +}
  277 +
266 - (CGFloat) mappingIsForAxisX : (BOOL) isForAxisX WithValue : (CGFloat) value{ 278 - (CGFloat) mappingIsForAxisX : (BOOL) isForAxisX WithValue : (CGFloat) value{
267 279
268 if (isForAxisX) { 280 if (isForAxisX) {
@@ -393,7 +405,9 @@ @@ -393,7 +405,9 @@
393 405
394 // call the same method on a background thread 406 // call the same method on a background thread
395 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 407 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
396 - [NSThread sleepForTimeInterval:2]; 408 + if (self.displayAnimated) {
  409 + [NSThread sleepForTimeInterval:2];
  410 + }
397 // calculating start and end point 411 // calculating start and end point
398 __block CGFloat startX = [self mappingIsForAxisX:true WithValue:startPoint.x]; 412 __block CGFloat startX = [self mappingIsForAxisX:true WithValue:startPoint.x];
399 __block CGFloat startY = [self mappingIsForAxisX:false WithValue:startPoint.y]; 413 __block CGFloat startY = [self mappingIsForAxisX:false WithValue:startPoint.y];
@@ -411,14 +425,21 @@ @@ -411,14 +425,21 @@
411 shapeLayer.lineWidth = lineWidth; 425 shapeLayer.lineWidth = lineWidth;
412 shapeLayer.fillColor = [color CGColor]; 426 shapeLayer.fillColor = [color CGColor];
413 // adding animation to path 427 // adding animation to path
414 - CABasicAnimation *animateStrokeEnd = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 428 + [self addStrokeEndAnimationIfNeededToLayer:shapeLayer];
415 - animateStrokeEnd.duration = _duration;  
416 - animateStrokeEnd.fromValue = [NSNumber numberWithFloat:0.0f];  
417 - animateStrokeEnd.toValue = [NSNumber numberWithFloat:1.0f];  
418 - [shapeLayer addAnimation:animateStrokeEnd forKey:nil];  
419 [self.layer addSublayer:shapeLayer]; 429 [self.layer addSublayer:shapeLayer];
420 }); 430 });
421 }); 431 });
422 } 432 }
423 433
  434 +- (void)addStrokeEndAnimationIfNeededToLayer:(CAShapeLayer *)shapeLayer{
  435 +
  436 + if (self.displayAnimated) {
  437 + CABasicAnimation *animateStrokeEnd = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
  438 + animateStrokeEnd.duration = _duration;
  439 + animateStrokeEnd.fromValue = [NSNumber numberWithFloat:0.0f];
  440 + animateStrokeEnd.toValue = [NSNumber numberWithFloat:1.0f];
  441 + [shapeLayer addAnimation:animateStrokeEnd forKey:nil];
  442 + }
  443 +}
  444 +
424 @end 445 @end