创建 2 个贝塞尔曲线路径的并集

问题描述 投票:0回答:5

我有两条贝塞尔路径,我想将它们组合起来形成一个联合,这样我就可以抚摸整个外部形状。就我而言,它是一个带有尾巴的语音气泡,因此尽管它不是一个复杂的形状,但实际上很难使用一条路径创建它。

似乎没有用于创建联合的 Core Graphics API。难道我错了?

如果我不是,有人知道可以处理这个问题的图书馆吗?我在 GitHub 上搜索过,没有结果。

ios swift core-graphics
5个回答
5
投票

如果您正在使用闭合形状,UIBezierPath 会执行此操作。

UIBezierPath *firstPath = [UIBezierPath bezierPath];
// build your path

UIBezierPath *secondPath = [UIBezierPath bezierPath];
// build your path

[firstPath appendPath:secondPath];

3
投票

如果您的部署目标是 iOS 16(或 macOS 13)或更高版本,您可以使用

CGPath
 方法计算两个 
union
对象的并集,如下所示:

let path1: CGPath = ...
let path2: CGPath = ...
let combo = path1.union(path2)

0
投票

您可以尝试使用 iOverlay 库: https://github.com/iShape-Swift/iOverlay

它专门为多边形(路径)布尔运算(如并集、差集等)而设计


-2
投票

在 Swift 3 中,Bezier 路径可以通过以下方式统一:

 override func draw(_ rect: CGRect) {
    super.draw(rect)

    UIColor.black.setStroke()
    UIColor.red.setFill()

    let currentContext = UIGraphicsGetCurrentContext()
    currentContext?.saveGState() 

    let path = drawTopView()
    path.lineWidth = 5.0
    path.fill()
    path.stroke()

    let middlepath = drawMiddleView()
    middlepath.lineWidth = 2.0
    middlepath.fill()
    middlepath.stroke()

    path.append(middlepath)
    currentContext?.restoreGState()
}

-2
投票

SwiftUI

func createView() -> some View {
    let a = Path { path in
        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: 50, y: 100))
        path.addLine(to: CGPoint(x: 100, y: 0))
    }
    .stroke(Color.white, style: StrokeStyle(lineWidth: 5, lineCap: .round, lineJoin: .round))

    let b = RoundedRectangle(cornerRadius: 5, style: .continuous)
        .fill(Color.white)
        .frame(width: 30, height: 30)
        .position(CGPoint.zero)

    return ZStack {
        a
        b
    }
    .compositingGroup()
    .colorMultiply(Color.red)
}
© www.soinside.com 2019 - 2024. All rights reserved.