title: How can I change the phase of UIColor patternImage in Swift?
body: <p>I'm using the very convenient <code>UIColor(patternImage:)</code> to create some <code>CAShapeLayer</code>s with tiled patterns in an iOS 10 app with Xcode 8.2. Tiling always starts at the origin of the view, which can be inconvenient if you want it to start somewhere else. To illustrate, here's a screenshot from the simulator (code below):</p>
  
  <p><a href="https://i.stack.imgur.com/TkZmD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TkZmD.png" alt="Tiling problem"></a></p>
  
  <p>The <code>CAShapeLayer</code> on the left starts at (0,0), so everything is fine. The one on the right is at (110,50), so it's split in the middle. Here's the code:</p>
  
  <pre><code>let firstBox = CAShapeLayer()
  firstBox.fillColor = UIColor(patternImage: UIImage(named: "test-image")!).cgColor
  view.layer.addSublayer(firstBox)
  firstBox.path = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 100, height: 100)).cgPath
  
  let secondBox = CAShapeLayer()
  secondBox.fillColor = UIColor(patternImage: UIImage(named: "test-image")!).cgColor
  view.layer.addSublayer(secondBox)
  secondBox.path = UIBezierPath(rect: CGRect(x: 110, y: 50, width: 100, height: 100)).cgPath
  </code></pre>
  
  <p>I want to adjust the phase of the pattern for the right <code>CAShapeLayer</code> so that both tiles show a full face. Apple's <a href="https://developer.apple.com/reference/uikit/uicolor/1621933-init" rel="nofollow noreferrer">documentation</a> for UIColor(patternImage:) helpfully refers to a function for this purpose:</p>
  
  <blockquote>
    <p>To change the phase, make the color the current color and then use the
    <a href="https://developer.apple.com/reference/coregraphics/cgcontext/1455334-setpatternphase" rel="nofollow noreferrer">setPatternPhase(_:)</a> function to change the phase.</p>
  </blockquote>
  
  <p>Sounds simple! But I'm having a hard time implementing it. I'm not really sure what "make the color the current color" means. I tried getting the current context and calling <code>setPatternPhase</code> on it, both before and after assigning the fill color to the layer:</p>
  
  <pre><code>UIGraphicsGetCurrentContext()?.setPatternPhase(CGSize(width: 25, height: 25))
  </code></pre>
  
  <p>No noticeable effect. I tried subclassing the containing <code>UIView</code> and setting the phase in its <code>drawRect:</code> method, as suggested in <a href="https://stackoverflow.com/a/10808484/149097">this answer</a>. But <code>drawRect:</code> doesn't exist in Swift, so I tried both <code>draw(_ rect:)</code> and <code>draw(_ layer:, in:)</code>. Both functions get called, but there's no noticeable effect.</p>
  
  <pre><code>class PatternView: UIView {
      override func draw(_ rect: CGRect) {
          UIGraphicsGetCurrentContext()?.setPatternPhase(CGSize(width: 25, height: 25))
          super.draw(rect)
      }
      override func draw(_ layer: CALayer, in ctx: CGContext) {
          ctx.setPatternPhase(CGSize(width: 25, height: 25))
          super.draw(layer, in: ctx)
      }
  }
  </code></pre>
  
  <p>At Dave Weston's suggestion, I used <code>UIImage</code>'s <code>.set()</code> to set the current stroke and fill for the current context before calling <code>setPatternPhase</code>. Unfortunately the output is unaffected. Here's the code I tried:</p>
  
  <pre><code>let secondBoxColor = UIColor(patternImage: UIImage(named: "test-image")!)
  secondBoxColor.set()
  UIGraphicsGetCurrentContext()?.setPatternPhase(CGSize(width: 50, height: 50))
  
  let secondBox = CAShapeLayer()
  secondBox.fillColor = secondBoxColor.cgColor
  view.layer.addSublayer(secondBox)
  secondBox.path = UIBezierPath(rect: CGRect(x: 110, y: 50, width: 100, height: 100)).cgPath
  </code></pre>
  
  <p><strong>How can I shift the phase of the pattern that gets drawn into a <code>CAShapeLayer</code>?</strong></p>