I have the following code below: basically this is one PieChart fragment with many of these fragments. Each slice is drawn in it by its own CALayer and is added to the user-level views using addSublayer:
The problem is that I dynamically update the pie chart when the user drags his finger (they can edit the values โโof the pie chart by dragging and dropping). It works well, but there is a very noticeable lag when re-drawing these "pieces" of cake. I look at the iOS profiling tool and it shows that> 50% of the time is in CGContextDrawPath() , because it has to redraw a piece of cake every time the user moves a certain number of degrees.
My question is: what can I do to improve the speed of this code? Is something missing?
Also, as a side note, this code works well on the iPad 2 (acceptable frame rates per second), but on the iPad 3 it is terribly scary, according to my estimates it is 2 times slower. Can someone explain this? Is this just a retina screen?
-(void)drawInContext:(CGContextRef)ctx { // Create the path CGRect insetBounds = CGRectInset(self.bounds, self.strokeWidth, self.strokeWidth); CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2); CGFloat radius = MIN(insetBounds.size.width/2, insetBounds.size.height/2); CGContextBeginPath(ctx); CGContextMoveToPoint(ctx, center.x, center.y); CGPoint p1 = CGPointMake(center.x + radius * cosf(self.startAngle), center.y + radius * sinf(self.startAngle)); CGContextAddLineToPoint(ctx, p1.x, p1.y); int clockwise = self.startAngle > self.endAngle; CGContextAddArc(ctx, center.x, center.y, radius, self.startAngle, self.endAngle, clockwise); CGContextClosePath(ctx); // Color it CGContextSetFillColorWithColor(ctx, self.fillColor.CGColor); CGContextSetStrokeColorWithColor(ctx, self.strokeColor.CGColor); CGContextSetLineWidth(ctx, self.strokeWidth); CGContextDrawPath(ctx, kCGPathFillStroke); }