tags:

views:

993

answers:

3

Hello,

I am trying to implement simple paint functionality in my iPhone app. I tried updating a bitmap with a bitmap brush, and I also tried this tutorial.

Both methods have the same problem, even though the code is almost totally different. It happens only on the device - the simulator works fine.

When I touch the screen and move my finger, the screen does not get updated. When I pause or lift my finger, then the screen gets updated. This is not a very good user experience!

I tried calling drawRect from touchesMoved directly, but found that the drawing context (which I retrieve using UIGraphicsGetCurrentContext) is invalid for many of the calls, so painting the screen myself for every touchesMoved doesn't work.

Any ideas?

Thanks for any help, this has been quite frustrating!

Henning

A: 

You can't directly call drawRect:. To refresh your screen on demand, try calling [self setNeedsDisplay] from your touchesMoved method, which will setup the proper contexts for a call to drawRect:.

drewh
Thanks, but I tried doing a [self setNeedsDisplay] from touchesMoved. It didn't help.
+2  A: 

It sounds to me like you're not giving the main run loop a chance to update the display. Your drawing code may be taking longer to execute than the time between touch events, so the display is never updated. When you lift your finger, it does the updating because it's no longer burdened with your drawing.

You might consider optimizing your drawing to speed it up (drawing only within the dirty region of the screen, for example), using something like NSOperationQueue to queue up the heavy calculations of your drawing to run on a background thread, or selectively dropping touch drawing events to keep your response smooth.

One additional possibility is placing your heavy drawing code in a separate method and calling it via performSelector:withObject:afterDelay, with a 10 millisecond (or smaller) delay. This might give the main run loop a chance to update the display with its current state. I haven't tested this, but if I remember correctly I've seen this work.

Brad Larson
I'll give that a try and see how it goes. Thank-you.
A: 

Henning, did you ever find a solution. I've got the same problem, if you draw too fast it looks .. er well rubbish :-/ I added the [self setNeedsDisplay] but like you it didn't make any difference.

No, never found a solution. I moved on to working on something else.