2

Reading through the documentation, it's not clear to me if overriding a UIView's drawRect or using layers is appropriate.

I am going to be rendering two kinds of graphs. A line graph and a single-bar bar graph. Both will have ticks along the axis and text aligned with the ticks. Below I only show four ticks per axis, but there could be more.

  1. What is the better way of drawing the ticks? Should I use an individual layer for each tick, or render them all at once on a separate view using drawRect?

  2. Is there another way to render the text other than using a separate UILabel for each?

  3. For the bar graph, I am using a CAGradientLayer for the bar. For the line graph, is it even possible to render this using layers?

Sample Graph

enter image description here

Benedict Cohen
  • 11,912
  • 7
  • 55
  • 67
Brian
  • 6,910
  • 8
  • 44
  • 82
  • 7
    There's an WWDC talk that discusses the graphs in Stocks.app. It's called Practical Drawing for iOS Developers and is from WWDC 2011. – Benedict Cohen Aug 07 '12 at 17:14
  • 3
    You can also look at [CAShapeLayer](http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CAShapeLayer_class/Reference/Reference.html) for the line graph. – David Rönnqvist Aug 07 '12 at 17:20
  • @DavidRönnqvist CAShapeLayer looks like exactly what I need. From what I've read, CALayer is highly optimized and has all sorts of goodies for doing animation that rendering manually doesn't make sense unless you *can't* do it with CALayer. – Brian Aug 07 '12 at 17:21
  • 1
    Link from Benedict's suggestion: https://developer.apple.com/videos/wwdc/2011/. Does not cover CALayer, just Quartz/Core Graphics. – Brian Aug 07 '12 at 18:21
  • 1
    If you want an example to work from, we built Core Plot: http://code.google.com/p/core-plot/ out of a hierarchy of CALayers so that only element that need to change were redrawn. There's still much that can be done to improve our rendering performance, but that was how we structured things. You want to minimize the amount of Quartz drawing you do when updating a plot, because it's fairly expensive and compositing CALayers is very cheap. – Brad Larson Aug 10 '12 at 03:13

1 Answers1

2

In the end, I used a combination of overriding drawRect and CALayer/CAShapeLayer. Having now been through the process of implementing it all from scratch, I would go with shapes and layers.

The shapes and layers approach is plenty performant, and is a more robust solution. In the end it takes less code if you are doing things like animation.

Brian
  • 6,910
  • 8
  • 44
  • 82