Swift 2 UIBezierPath的绘图差异

Swift 2 UIBezierPath的绘图差异,swift,core-graphics,uibezierpath,Swift,Core Graphics,Uibezierpath,我想在2个UIBezierPath之间绘制差异(参见屏幕截图),以便只绘制圆角,正如您在我的屏幕截图(图C)上看到的那样 这是我的密码: let context = UIGraphicsGetCurrentContext() CGContextSaveGState(context) let rectanglePath = UIBezierPath(rect: rect) CGContextAddPath(context, rectanglePath.CGPath) CGContextEOCl

我想在2个UIBezierPath之间绘制差异(参见屏幕截图),以便只绘制圆角,正如您在我的屏幕截图(图C)上看到的那样

这是我的密码:

let context = UIGraphicsGetCurrentContext()
CGContextSaveGState(context)

let rectanglePath = UIBezierPath(rect: rect)
CGContextAddPath(context, rectanglePath.CGPath)
CGContextEOClip(context)

let roundedRectanglePath = UIBezierPath(roundedRect: productRect, byRoundingCorners: roundedCorners, cornerRadii: CGSize(width: 6, height: 6))
CGContextAddPath(context, roundedRectanglePath.CGPath)
CGContextFillPath(context)

CGContextRestoreGState(context)
不幸的是,它不起作用。我只画圆形的黑色矩形

你有什么想法吗

非常感谢。

您可以使用一条路径:

let path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
path.append(UIBezierPath(rect: bounds))
path.usesEvenOddFillRule = true

UIColor.black.setFill()
path.fill()
也可以使用CoreGraphics:

let path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
path.append(UIBezierPath(rect: bounds))

let context = UIGraphicsGetCurrentContext()!
context.addPath(path.cgPath)
context.setFillColor(UIColor.black.cgColor)
context.fillPath(using: .evenOdd)
这将产生:


请参阅有关Swift 2格式副本的答案。

这太完美了!非常感谢,罗布。