title: SwiftUI add inverted mask
body: <p>I'm trying to add a mask to two shapes such that the second shape masks out the first shape. If I do something like <code>Circle().mask(Circle().offset(…))</code>, this has the opposite affect: preventing anything outside the first circle from being visible.</p>
  
  <p>For <code>UIView</code> the answer is here: <a href="https://stackoverflow.com/questions/14411765/ios-invert-mask-in-drawrect">iOS invert mask in drawRect</a></p>
  
  <p>However, trying to implement this in SwiftUI <em>without</em> <code>UIView</code> eludes me. I tried implementing an InvertedShape with I could then use as a mask:</p>
  
  <pre class="lang-swift prettyprint-override"><code>struct InvertedShape&lt;OriginalType: Shape&gt;: Shape {
      let originalShape: OriginalType
  
      func path(in rect: CGRect) -&gt; Path {
          let mutableOriginal = originalShape.path(in: rect).cgPath.mutableCopy()!
          mutableOriginal.addPath(Path(rect).cgPath)
          return Path(mutableOriginal)
              .evenOddFillRule()
      }
  }
  </code></pre>
  
  <p>Unfortunately, SwiftUI paths do not have <code>addPath(Path)</code> (because they are immutable) or <code>evenOddFillRule()</code>. You can access the path's CGPath and make a mutable copy and then add the two paths, however, <code>evenOddFillRule</code> needs to be set on the <code>CGLayer</code>, not the <code>CGPath</code>. So unless I can get to the CGLayer, I'm unsure how to proceed.</p>
  
  <p>This is Swift 5.</p>