Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimizing a drawing (with finger touches) application for iPhone SDK

I'm writing an application that uses your finger to draw simple diagrams. I have it working for the most part but now I'm trying to optimize its performance. When the user swipes their finger fast, I can't capture enough touch events to draw a smooth path.

Here's my current approach:

1) I subclassed a UIView and added a poroperty to a CGLayer (gets created lazily and is the same size as my UIView). 2) My UIView subclass responds to touch events by storing the current and last touch points in instance variables. 3) My view's setNeedsDisplay is called and in the draw rect , do the following: - draw a line from the previous touch location to the current touch location to the CGLayer - draw the entire CGLayer to my views context in one go

The main problem is when a user swipes fast I get relatively few touch events, so the lines I draw between the touches are long and makes the path look jagged not smooth.

My questions:

1) Does drawRect (on my UIView subclass) and my touch event handlers on my UIView subclass get called in the same thread? I.e. could I have to threads executing (one in a touch event and the second in my draw rect)?

If no - do touch events get queued up while drawRect is being called? And how can I improve performance - simply improve performance of drawRect?

If yes - how can I get more touch events to happen so I can draw a smoother path?

Thanks.

like image 630
aloo Avatar asked Jul 14 '09 03:07

aloo


2 Answers

Another approach is to interpolate the curve between the sample points. When the finger drag starts, begin collecting sample points. As the number of points increase, redraw the line. With two points, draw a straight line, with three or more draw a curve. You can re-start the process when two points are sampled that lie within a defined distance. This would allow you to draw two arcs (like a 'm') in one motion - you naturally pause in the middle as you change direction, possibly long enough for two or more samples.

like image 83
Skizz Avatar answered Sep 17 '22 22:09

Skizz


drawRect gets called on the main thread. But you don't have to do this. You can use the main thread to collect UI events and do the drawing on a background thread. The background thread gets notified whenever there are new touches and starts a drawing operation in its own CGBitmapContext. Then you create a CGImage and hand it over to the View: view.layer.contents = drawingImage.

If you need even more performance, consider drawing using OpenGL.

like image 33
Nikolai Ruhe Avatar answered Sep 17 '22 22:09

Nikolai Ruhe