Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing many UIBezierPaths in a view

I am drawing several UIBezierPaths on a view based on finger movements.

Every time a cycle of touches -- Began/Moved/Ended -- completes, I store the points and create a UIBezierPath that is stored in an array called bezierArray. I have another array called bezierArrayColors that stores the colors of each path.

The problem is this. The class uses drawRect. As far as I can see, every time drawRect runs, it has to draw all the paths that were ever created and the app is now slow.

This is my drawRect now. I know it is pretty lame, but I am not seeing how this can be done.

- (void)drawRect:(CGRect)rect {
   for (int i=0; i<[self.bezierArray count]; i++) {
        UIBezierPath *aPath = (UIBezierPath*)[self.bezierArray objectAtIndex:i];
        UIColor *aColor = (UIColor*)[self.bezierArrayColor objectAtIndex:i];
    [aPath setLineWidth:LINE_WIDTH];

    [aColor setStroke];
    [aPath stroke];
    }
}

Is there a way to stroke a UIBezierPath with different colors or perhaps widths using subpaths? I mean, to change color, width and other properties of a subpath? That would allow me to use one UIBezierPath with several different subpaths. I wish one bezier could be drawn and left there without needing to be redrawn every time. What am I missing?

like image 870
Duck Avatar asked Jun 05 '11 01:06

Duck


2 Answers

Make sure that you pay attention to the rect that's passed into -drawRect:. If your code takes the easy way out and redraws the entire view every time -drawRect: is called, you may be doing far more drawing than necessary at least some of the time.

like image 123
Caleb Avatar answered Nov 08 '22 03:11

Caleb


Draw each bezier path in a separate subview. That way each bezier only has to be redrawn when it itself has changed.

like image 28
Wilbur Vandrsmith Avatar answered Nov 08 '22 04:11

Wilbur Vandrsmith