Antoni Silvestrovič
Antoni Silvestrovič

Reputation: 1035

How to combine shapes in SwiftUI?

I need to create a shape from several other shapes, and ideally, I'd get a single shape struct at the end which would have two shapes stacked as ZStack would. I haven't noticed an obvious implementation of this anywhere, so maybe somebody has some ideas on how to implement it?

Here's what I want to have:

struct CustomShape: Shape {
    func path(in rect: CGRect) -> Path {
        // Add shapes here???? For example, combine Rectangle with Circle?
    }
}

Upvotes: 7

Views: 4606

Answers (1)

Nicolas Mandica
Nicolas Mandica

Reputation: 861

You can use func addPath(_ path: Path, transform: CGAffineTransform = .identity) to create a new Shape like this :

struct CustomShape: Shape {
    func path(in rect: CGRect) -> Path {
        var customShape = Path { p in
            p.move(to: CGPoint(x: 0, y: 0))
            p.addQuadCurve(to: CGPoint(x: 0, y: 100),
                           control: CGPoint(x: 0, y: 0))
            p.addCurve(to: CGPoint(x: 100, y: 400),
                       control1: CGPoint(x: 0, y: 200),
                       control2: CGPoint(x: 100, y: 200))
            p.addCurve(to: CGPoint(x: 200, y: 100),
                       control1: CGPoint(x: 100, y: 200),
                       control2: CGPoint(x: 200, y: 200))
            p.addQuadCurve(to: CGPoint(x: 200, y: 0),
                           control: CGPoint(x: 200, y: 0))
        }

        let rectangle = Rectangle()
            .path(in: customShape.boundingRect)
            .offsetBy(dx: 100, dy: 0)

        customShape.addPath(rectangle, transform: .init(scaleX: 0.5, y: 0.35))

        return customShape
    }
}

And use it like this :

struct SwiftUIView: View {
    var body: some View {
        CustomShape()
    }
}

Upvotes: 8

Related Questions