0

I have three paths. I want two of those paths, path1 and path2, to be subtracted from path3. I do not want the area that overlaps between path1 and path2 to be filled. Here's a diagram I made to explain what I mean:

Diagram

I already tried this question, but it the accepted answer produces what is found above in "Result With EOClip." I tried CGContextSetBlendMode(ctx, kCGBlendModeClear), but all it did was make the fill black. Any ideas?

Community
  • 1
  • 1

1 Answers1

2

Playing a bit with PaintCode () I landed with this. Maybe it works for your case?

let context = UIGraphicsGetCurrentContext()
CGContextBeginTransparencyLayer(context, nil)

let path3Path = UIBezierPath(rect: CGRectMake(0, 0, 40, 40))
UIColor.blueColor().setFill()
path3Path.fill()

CGContextSetBlendMode(context, kCGBlendModeDestinationOut)
let path2Path = UIBezierPath(rect: CGRectMake(5, 5, 20, 20))
path2Path.fill()
let path1Path = UIBezierPath(rect: CGRectMake(15, 15, 20, 20))
path1Path.fill()

CGContextEndTransparencyLayer(context)
Dominique Vial
  • 3,729
  • 2
  • 25
  • 45
Stefan
  • 1,496
  • 1
  • 13
  • 26
  • First of all, the code works perfectly. However, I'm still a bit confused about saving and loading the GState twice. Does saving and restoring the GState work like a stack? – DefinitelyNotAPlesiosaur Jul 22 '15 at 17:48
  • 1
    Yes, https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGContext/index.html#//apple_ref/c/func/CGContextSaveGState "Pushes a copy of the current graphics state onto the graphics state stack for the context." – Stefan Jul 22 '15 at 17:51
  • 1
    As for saving the state twice, maybe that's just unnecessary boilerplate code that PaintCode.app produced it. Try removing it :) – Stefan Jul 22 '15 at 17:52
  • You can remove the inner creation of a new transparency layer and GState stuff. It's probably still there so you can draw on the first transparency layer after having done all of the stuff with `kCGBlendModeDesinationOut`. – DefinitelyNotAPlesiosaur Jul 22 '15 at 17:57