Drawing many UIBezierPaths in a view

I draw several UIBezierPath in a finger motion based view.

Each time the touch cycle - Start / Move / End - ends, I save the points and create a UIBezierPath , which is stored in an array called bezierArray . I have another array called bezierArrayColors that preserves the colors of each path.

The problem is this. The class uses drawRect . As far as I see, every time drawRect is executed, it should draw all the paths that have ever been created, and the application is now slow.

Now this is my drawRect . I know this is pretty lame, but I don't see 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 UIBezierPath different colors or possibly width using subfolders? I mean, to change the color, width and other properties of a subpath? This would allow me to use one UIBezierPath with several different subpaths. I would like one bezier to be drawn and left there, without having to redraw every time. What am I missing?

+6
source share
3 answers

Make sure you pay attention to the rectangle that was passed in -drawRect :. If your code makes a simple exit and redraws the whole view each time when -drawRect is called: you can do much more drawing than is necessary, at least for a while.

+6
source

Draw each bezier path in a separate preview. Thus, each bezier should be redrawn only when he himself has changed.

+2
source

I have a similar problem, and I plan to use subview to store all the "completed" paths and another subheading, so that only the path is in the process. This way, I don’t need to draw all completed paths, since I get new events related events for the progress path (s). As soon as the path is completed, I will move it to the completed array, redo the completed preview and wait for the next touch. This avoids the problem of β€œtrillions of sub-lives,” and also avoids redrawing the entire array of paths, while actually trying to respond to touches that are very sensitive to delays. As soon as I do this, I will try to remember to return here with some code.

0
source

Source: https://habr.com/ru/post/889798/


All Articles