MrWooJ

drawing UI methods on the background thread

@@ -181,8 +181,8 @@ @@ -181,8 +181,8 @@
181 - (void)setChartData:(NSArray *)data 181 - (void)setChartData:(NSArray *)data
182 { 182 {
183 if (data != _chartData) { 183 if (data != _chartData) {
184 - CGFloat yFinilizeValue , xFinilizeValue; 184 + __block CGFloat yFinilizeValue , xFinilizeValue;
185 - CGFloat yValue , xValue; 185 + __block CGFloat yValue , xValue;
186 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 186 CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
187 pathAnimation.duration = _duration; 187 pathAnimation.duration = _duration;
188 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 188 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
@@ -191,22 +191,28 @@ @@ -191,22 +191,28 @@
191 pathAnimation.fillMode = kCAFillModeForwards; 191 pathAnimation.fillMode = kCAFillModeForwards;
192 self.layer.opacity = 1; 192 self.layer.opacity = 1;
193 193
194 - for (PNScatterChartData *chartData in data) { 194 + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
195 - for (NSUInteger i = 0; i < chartData.itemCount; i++) { 195 + [NSThread sleepForTimeInterval:1];
196 - yValue = chartData.getData(i).y; 196 + // update UI on the main thread
197 - xValue = chartData.getData(i).x; 197 + dispatch_async(dispatch_get_main_queue(), ^{
198 - if (!(xValue >= _AxisX_minValue && xValue <= _AxisX_maxValue) || !(yValue >= _AxisY_minValue && yValue <= _AxisY_maxValue)) { 198 + for (PNScatterChartData *chartData in data) {
199 - NSLog(@"input is not in correct range."); 199 + for (NSUInteger i = 0; i < chartData.itemCount; i++) {
200 - exit(0); 200 + yValue = chartData.getData(i).y;
  201 + xValue = chartData.getData(i).x;
  202 + if (!(xValue >= _AxisX_minValue && xValue <= _AxisX_maxValue) || !(yValue >= _AxisY_minValue && yValue <= _AxisY_maxValue)) {
  203 + NSLog(@"input is not in correct range.");
  204 + exit(0);
  205 + }
  206 + xFinilizeValue = [self mappingIsForAxisX:true WithValue:xValue];
  207 + yFinilizeValue = [self mappingIsForAxisX:false WithValue:yValue];
  208 + CAShapeLayer *shape = [self drawingPointsForChartData:chartData AndWithX:xFinilizeValue AndWithY:yFinilizeValue];
  209 + [self.layer addSublayer:shape];
  210 + self.pathLayer = shape ;
  211 + [self.pathLayer addAnimation:pathAnimation forKey:@"fade"];
  212 + }
201 } 213 }
202 - xFinilizeValue = [self mappingIsForAxisX:true WithValue:xValue]; 214 + });
203 - yFinilizeValue = [self mappingIsForAxisX:false WithValue:yValue]; 215 + });
204 - CAShapeLayer *shape = [self drawingPointsForChartData:chartData AndWithX:xFinilizeValue AndWithY:yFinilizeValue];  
205 - [self.layer addSublayer:shape];  
206 - self.pathLayer = shape ;  
207 - [self.pathLayer addAnimation:pathAnimation forKey:@"fade"];  
208 - }  
209 - }  
210 } 216 }
211 } 217 }
212 218
@@ -313,27 +319,35 @@ @@ -313,27 +319,35 @@
313 } 319 }
314 320
315 - (void) drawLineFromPoint : (CGPoint) startPoint ToPoint : (CGPoint) endPoint WithLineWith : (CGFloat) lineWidth AndWithColor : (UIColor*) color{ 321 - (void) drawLineFromPoint : (CGPoint) startPoint ToPoint : (CGPoint) endPoint WithLineWith : (CGFloat) lineWidth AndWithColor : (UIColor*) color{
316 - // calculating start and end point 322 +
317 - CGFloat startX = [self mappingIsForAxisX:true WithValue:startPoint.x]; 323 + // call the same method on a background thread
318 - CGFloat startY = [self mappingIsForAxisX:false WithValue:startPoint.y]; 324 + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
319 - CGFloat endX = [self mappingIsForAxisX:true WithValue:endPoint.x]; 325 + [NSThread sleepForTimeInterval:2];
320 - CGFloat endY = [self mappingIsForAxisX:false WithValue:endPoint.y]; 326 + // calculating start and end point
321 - // drawing path between two points 327 + __block CGFloat startX = [self mappingIsForAxisX:true WithValue:startPoint.x];
322 - UIBezierPath *path = [UIBezierPath bezierPath]; 328 + __block CGFloat startY = [self mappingIsForAxisX:false WithValue:startPoint.y];
323 - [path moveToPoint:CGPointMake(startX, startY)]; 329 + __block CGFloat endX = [self mappingIsForAxisX:true WithValue:endPoint.x];
324 - [path addLineToPoint:CGPointMake(endX, endY)]; 330 + __block CGFloat endY = [self mappingIsForAxisX:false WithValue:endPoint.y];
325 - CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 331 + // update UI on the main thread
326 - shapeLayer.path = [path CGPath]; 332 + dispatch_async(dispatch_get_main_queue(), ^{
327 - shapeLayer.strokeColor = [color CGColor]; 333 + // drawing path between two points
328 - shapeLayer.lineWidth = lineWidth; 334 + UIBezierPath *path = [UIBezierPath bezierPath];
329 - shapeLayer.fillColor = [color CGColor]; 335 + [path moveToPoint:CGPointMake(startX, startY)];
330 - // adding animation to path 336 + [path addLineToPoint:CGPointMake(endX, endY)];
331 - CABasicAnimation *animateStrokeEnd = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 337 + CAShapeLayer *shapeLayer = [CAShapeLayer layer];
332 - animateStrokeEnd.duration = _duration; 338 + shapeLayer.path = [path CGPath];
333 - animateStrokeEnd.fromValue = [NSNumber numberWithFloat:0.0f]; 339 + shapeLayer.strokeColor = [color CGColor];
334 - animateStrokeEnd.toValue = [NSNumber numberWithFloat:1.0f]; 340 + shapeLayer.lineWidth = lineWidth;
335 - [shapeLayer addAnimation:animateStrokeEnd forKey:nil]; 341 + shapeLayer.fillColor = [color CGColor];
336 - [self.layer addSublayer:shapeLayer]; 342 + // adding animation to path
  343 + CABasicAnimation *animateStrokeEnd = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
  344 + animateStrokeEnd.duration = _duration;
  345 + animateStrokeEnd.fromValue = [NSNumber numberWithFloat:0.0f];
  346 + animateStrokeEnd.toValue = [NSNumber numberWithFloat:1.0f];
  347 + [shapeLayer addAnimation:animateStrokeEnd forKey:nil];
  348 + [self.layer addSublayer:shapeLayer];
  349 + });
  350 + });
337 } 351 }
338 352
339 @end 353 @end