2

In my application, I have a UIViewController with a subclassed UIView (and several other elements) inside of it. Inside of the UIView, called DrawView, in my drawRect: method, I draw a table grid type thing, and plot an array of CGPoints on the grid. When the user taps on the screen, it calls touchesBegan:withEvent: and checks to find the closest point on the grid to the touch, adds a point to the array that the drawRect: method draws points from, and calls [self setNeedsDisplay]. As the user moves their finger around the screen, it checks to see if the point changed from the last location, and updates the point and calls [self setNeedsDisplay] as necessary.

This works great in the Simulator. However, when run on a real iPhone, it runs very slowly, when you move your finger around, it lags in drawing the dot. I have read that running calculations for where to place the points in a different thread can improve performance. Does anyone have experience with this that knows this for a fact? Any other suggestions to reduce lag?

Josh Sherick
  • 2,161
  • 3
  • 20
  • 37

1 Answers1

4

Any other suggestions to reduce lag?

Yes. Don't use -drawRect:. It's a long and complicated reason why, but basically when UIKit sees that you've implemented -drawRect: in your UIView subclass, rendering goes through the really slow software-based rendering path. When you draw with CALayer objects and composite views, you can get hardware accelerated graphics, which can make your app FAR more performant.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • Great answer. But how would you do that? User different view each with a layer or one view with multiple sublayers? – Sandro Meier Aug 09 '11 at 07:21
  • @Sandro either would work, but I would probably use `UIView` objects instead of `CALayers`, because that leaves the possibility of attaching gesture recognizers, dealing with ObjC types instead of CF types (UIImage vs CGImageRef, etc)... – Dave DeLong Aug 09 '11 at 13:26
  • Thanks. I'm kind of newish and I have absolutely no idea what CALayer objects or composite views are. I found [Apple's documentation on CALayer](http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html), but are there any other tutorials or documentation that you recommend learning about these things, and how to use them to draw with? – Josh Sherick Aug 09 '11 at 21:17
  • @josherick By "composite views", I mean "add views as subviews of other views". You're compositing them together, i.e. layering them on top of each other. ("composite" is being used as a verb, not an adjective) – Dave DeLong Aug 09 '11 at 22:10
  • wait, really? that seems like it would take a lot of memory, drawing a grid out of UIViews. and how would i draw lines connecting the points? or even a circle (point)? – Josh Sherick Aug 09 '11 at 23:19
  • @Josherick yeah, there's a bit of a memory penalty for doing this, but it's MUCH faster. As for drawing a grid, you can draw that into an image, and then slap the image into a `UIImageView`, and then never have to redraw it again. – Dave DeLong Aug 09 '11 at 23:25