I am drawing several UIBezierPath
s 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?
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.
Draw each bezier path in a separate subview. That way each bezier only has to be redrawn when it itself has changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With