Swiftpack.co -  mkj-is/PathBuilder as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
mkj-is/PathBuilder
SwiftUI result builder for Path
.package(url: "https://github.com/mkj-is/PathBuilder.git", from: "2.0.0")
PathBuilder logo

PathBuilder

Path builder is a complete result builder for lifting Path into the declarative SwiftUI world. This @resultBuilder can be used for elegant and short definition of paths. Missing documentation gaps in SwiftUI are filled in using the old but good CGMutablePath knowledge.

Motivation

I just wanted to learn to implement result builders. And during playing with animated paths in SwiftUI found a perfect place to experiment.

Usage

Examples

With PathBuilder you can write this to draw a triangle:

Path {
    Move(to: CGPoint(x: 50, y: 50))
    Line(to: CGPoint(x: 100, y: 100))
    Line(to: CGPoint(x: 0, y: 100))
    Close()
}

Instead of longer version:

Path { p in
    p.move(to: CGPoint(x: 50, y: 50))
    p.addLine(to: CGPoint(x: 100, y: 100))
    p.addLine(to: CGPoint(x: 0, y: 100))
    p.closeSubpath()
}

Drawing a Swift logo can be implemented like this.

Swift logo path drawing using PathBuilder

Path components

There are many basic path components present. You can create a new one by conforming to the PathComponent protocol.

Elementary components

  • Arc – Adds an arc of a circle to the path, specified with a radius and angles.
  • Close – Closes and completes a subpath in a path.
  • Curve – Adds a cubic Bézier curve to the path, with the specified end point and control points.
  • Oval – Adds an ellipse that fits inside the specified rectangle.
  • EmptySubpath – Adds empty subpath, used mainly as a temporary placeholder.
  • Line – Appends a straight line segment from the current point to the specified point.
  • Lines – Adds a sequence of connected straight-line segments to the path.
  • Move – Begins a new subpath at the specified point.
  • QuadCurve – Adds a quadratic Bézier curve to the path, with the specified end point and control point.
  • Rect – Adds a set of rectangular subpaths to the path.
  • RelativeArc – Adds an arc of a circle to the path, specified with a radius and a difference in angle.
  • RoundedRect – Adds a subpath to the path, in the shape of a rectangle with rounded corners.
  • TangentArc – Adds an arc of a circle to the path, specified with a radius and two tangent lines.

Grouping components

  • Loop – Appends components to path iterating over supplied sequence and building path for each element.
  • Subpath – Groups and appends another subpath object to the path and optionally transforms it.

Requirements

For Swift 5.1 to 5.3 use package version 1.1.1.

Otherwise for version 2.0+ use latest tools:

  • Xcode 12.5 or above
  • Swift 5.4 or above
  • iOS 13, macOS 10.15, watchOS 6.0, tvOS 13.0 or above

Installation

Using Swift Package Manager in Xcode or by adding to your Package manifest file.

Contributing

All contributions are welcome.

Project was created by Matěj Kašpar Jirásek.

Project is licensed under MIT license.

GitHub

link
Stars: 43
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

@resultBuilder release
1 week ago

Swift 5.4 and Xcode 12.5 support. Not compatible with older Swift versions, if you want to use this library with older Swift use version 1+.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API