UIBezierPath附加重叠未填充

Loo*_*vjo 2 append uiview uibezierpath swift

我正在尝试合并两个重叠的UIBezierPaths UIBezierPath.append,我希望填充重叠空间.我尝试将usesEvenOddFillRule属性设置为false,但它仍然没有填充.这是问题的最小例子:

override func draw(_ rect: CGRect) {
    let firstShape = UIBezierPath()

    firstShape.move(to: CGPoint(x: 100, y: 100))
    firstShape.addLine(to: CGPoint(x: 100, y: 150))
    firstShape.addLine(to: CGPoint(x: 150, y: 170))
    firstShape.close()

    let secondShape = UIBezierPath(rect: CGRect(x: 125, y: 125, width: 75, height: 75))

    let combined = UIBezierPath()
    combined.append(firstShape)
    combined.append(secondShape)

    UIColor.black.setFill()
    combined.fill()
}
Run Code Online (Sandbox Code Playgroud)

这产生以下形状:

形状

我希望它看起来像:

想要的形状

move(to: CGPoint)在一个UIBezierPath上使用时似乎也会出现此问题.如果要在同一个UIBezierPath上绘制这两个形状,则会出现同样的问题.

有谁知道如何填补重叠区域?优选地,解决方案在进行时也可以工作addClip()

vac*_*ama 7

你在正确的轨道上设置usesEvenOddFillRule为假.除此之外,您还需要确保您的形状都以相同的方向(顺时针或逆时针)绘制.

addLine当绘制三角形以反转它时,我颠倒了你的顺序.

override func draw(_ rect: CGRect) {
    let firstShape = UIBezierPath()

    firstShape.move(to: CGPoint(x: 100, y: 100))
    firstShape.addLine(to: CGPoint(x: 150, y: 170))
    firstShape.addLine(to: CGPoint(x: 100, y: 150))
    firstShape.close()

    let secondShape = UIBezierPath(rect: CGRect(x: 125, y: 125, width: 75, height: 75))

    let combined = UIBezierPath()
    combined.append(firstShape)
    combined.append(secondShape)
    combined.usesEvenOddFillRule = false

    UIColor.black.setFill()
    combined.fill()
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您发现重叠空间未填充,您也可以使用 secondaryShape.reversed() 。 (2认同)