title: iOS: Getting CGMutablePath not in drawRect: and Assign it to CAShapeLayer's Path
body: <p>I have been banging my head on this for hours. I tried to read up and searched for answers. I even tried the "walk-away-and-come-back-to-it-later" approach, it didn't work. I even tried Yoga and meditated! I didn't prevail.. </p>
  
  <p>So please enlighten me, experts.  </p>
  
  <p>All I am trying to get a copy of CGPath not inside drawRect:</p>
  
  <pre><code>CGMutablePathRef path = CGPathCreateMutable();
  CGPathMoveToPoint(path, ......);
  CGPathAddLineToPoint(path, ....);
  CGPathAddLineToPoint(path, ....);
  CGPathAddLineToPoint(path, ....);
  CGPathAddLineToPoint(path, ....);
  </code></pre>
  
  <p>And finally:<br>
  <code>
      CGPathCloseSubpath(path);<br>
      CGContextAddPath(context, path);<br>
  </code>
  That didn't work, so I tried also using instead of the 2 calls above:
  <code>
      CGPathCreateMutableCopy(path);<br>
  </code>
  And eventually - I just want to be able to get this path and insert it as path in CAShapeLayer and animate it by making this call:<br></p>
  
  <p><code>
      animation.toValue = [UIBezierPath bezierPathWithCGPath:path];<br>
  </code></p>
  
  <p>So before I'd try to take up another relaxing activity such as Sky Diving without a parachute in hopes to clear my mind and finally be able to get this right, could you please help me here by showing me some examples? In addition to that, can you explain to me in detail the correct way using the following functions:<br>
  <br>
  <code>
      CGPathCloseSubpath(path);<br>
      CGContextAddPath(context, path);<br>
      CGPathCreateMutableCopy(path);<br>
  </code></p>
  
  <p>Thanks in advance.</p>