title: Encapsulate drawRect gradients in CAGradientLayer
body: <p>There are dozens of excellent <a href="https://stackoverflow.com/questions/6696015/how-can-i-draw-a-sector-with-radial-gradient-iphone">examples</a> on here and elsewhere of how to use gradients by</p>
  
  <pre><code>-(void)drawRect:(CGRect)rect
  </code></pre>
  
  <p>However, I'm new to Quartz and it's likely I'm missing something. When I found <a href="http://danielbeard.wordpress.com/2012/02/25/gradient-background-for-uiview-in-ios/" rel="nofollow noreferrer">this</a>, it seems more intelligent to create a CAGradientLayer and then add it to my view with something like:</p>
  
  <pre><code>-(void)viewWillAppear
  {
     CAGradientLayer *bgLayer = [BackgroundLayer blueGradient];
     bgLayer.frame = self.view.bounds;
     [self.view.layer insertSublayer:bgLayer atIndex:0];
  }
  </code></pre>
  
  <p>Where the blueGradient is a separate class method (within BackgroundLayer.m) which creates the gradient, as follows: </p>
  
  <pre><code>+ (CAGradientLayer*) blueGradient {
  
      UIColor *colorOne   = [UIColor colorWithRed:(120/255.0) green:(135/255.0) blue:(150/255.0) alpha:1.0];
     UIColor *colorTwo   = [UIColor colorWithRed:(57/255.0)  green:(79/255.0)  blue:(96/255.0)  alpha:1.0];
  
     NSArray *colors     = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil];
     NSNumber *stopOne   = [NSNumber numberWithFloat:0.0];
     NSNumber *stopTwo   = [NSNumber numberWithFloat:1.0];
  
     NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, nil];
  
     CAGradientLayer *headerLayer = [CAGradientLayer layer];
     headerLayer.colors = colors;
     headerLayer.locations = locations;
  
     return headerLayer;
  
  }
  </code></pre>
  
  <p>So here's my question. What if instead of this simple linear gradient, I want to create a radial one? How can I extend blueGradient so that it can handle two dimensional gradients? Or what if I want to add gradients on top of gradients? the drawRect function seems so limiting.</p>
  
  <p>Or maybe that's just the wrong approach... then what's the wiring that I'm missing? How do I add gradients to my view(s) using drawRect? I want to be sure I'm doing it in a modular way so I can add gradient overlays, etc, as additional layers as necessary.</p>