Optimize drawing (using your fingers) iPhone SDK app

I am writing an application that uses your finger to draw simple diagrams. It works for me for the most part, but now I'm trying to optimize its performance. When the user swipes quickly, I cannot capture enough touch events to draw a smooth path.

Here is my current approach:

1) I have subclassed UIView and added a rocker to CGLayer (created lazily and has the same size as my UIView). 2) My subclass of UIView responds to touch events, storing the current and last touch points in instance variables. 3) My view of setNeedsDisplay is also called in draw rect, do the following: - draw a line from the previous touch location to the current location of the touch to CGLayer - draw the entire CGLayer in the context of my views at a time

The main problem is that the user quickly goes over me, I get a relatively small number of touch events, so the lines that I draw between touches are long and make the path look uneven.

My questions:

1) Is drawRect (in my UIView subclass) called and touch event handlers of my UIView subclass in the same thread called? That is, can I execute streams (one in a touch-screen event, and the second in my straight line)?

If there are no touch touch events, is it called in the queue while drawRect is being called? And how can I improve performance - just improve drawRect performance?

If so, how can I get more touch events so that I can make a smoother path?

Thanks.

+6
iphone uikit quartz-graphics drawing
source share
5 answers

Another approach is to interpolate the curve between the sample points. When you drag your finger, start collecting point samples. As the number of points increases, redraw the line. With two points, draw a straight line, with three or more draw a curve. You can restart the process when two points are selected that lie at a certain distance. This will allow you to draw two arcs (for example, "m") in one motion - you naturally stop in the middle when you change direction, perhaps long enough for two or more patterns.

+4
source share

drawRect is called in the main thread. But you don’t have to do it. You can use the main thread to collect user interface events and draw a picture on the background thread. The background thread is notified whenever new touches appear, and starts the drawing operation in its own CGBitmapContext. Then you create a CGImage and pass it to View: view.layer.contents = drawingImage .

If you need even more performance, consider using OpenGL.

+3
source share

Aloo, you found a solution for it, since I have the same problem. I also found a complicated tutorial http://www.ipodtouchfans.com/forums/showthread.php?t=132024 , but it also has the same problem that if you draw quickly, say, a circle, the drawing is not very smooth. This is almost the same as the iPhone just can’t keep up, unfortunately, this should use basic graphic materials.

+1
source share

I tried adding

 CGContextSetLineJoin(UIGraphicsGetCurrentContext(), kCGLineJoinRound); 

but it did nothing. It looks like we have to calculate the Bezier curves

0
source share

All Articles