跳转至
Structure

Path

The outline of a custom shape.

Declaration

@frozen struct Path : Equatable, LosslessStringConvertible

Overview

Use this structure to outline a custom shape.

Creating a path

There are 8 different ways to create a path:

  1. Empty path: init()
  2. Path from closure: init(_:)
  3. Path from CGMutablePath
  4. Path from CGPath
  5. Path from Rectangle
  6. Path from String
  7. Path from ellipse: init(ellipseIn:)
  8. Path from RoundedRectangle:

    1. init(roundedRect:cornerSize:style:)
    2. init(roundedRect:cornerRadius:style:)

Modifying and mutating a path

Use mutating path methods in conjunction with the init-from-a-closure path initializer to mutate the path before returning it. These methods mutate and return the path.

Use non-mutating (modifying) path methods on already existing paths similarly to view modifiers to update and return something new.

In the following example, move(to:) and addLine(to:) are mutating methods, while stroke() is a non-mutating method.

struct PathView: View {
    var body: some View {
        Path { path in
            path.move(to: .zero)
            path.addLine(to: CGPoint(x: 300, y: 300))
        }
        .stroke(Color.orange, lineWidth: 5)
    }
}
75F56224-5C30-4AE3-B526-336907676D1E

Using a path

The primary way to use a path is in conjunction with the path(in:) function. Create a custom path, and return it to this function in order to create a custom shape:

struct CustomShapeView: View {
    var body: some View {
        CustomShape()
            .stroke(Color.orange, linewidth: 10)
    }
}

struct CustomShape: Shape {
    func path(in rect: CGRect) -> Path {
        return Path(rect)
    }
}
CE1CCD64-157A-4908-B57D-96FC577A693D

Availability

iOS 13.0+

macOS 10.15+

tvOS 13.0+

watchOS 6.0+

Topics


Instance Property

boundingRect A property that contains the outer bounding rectangle of the path.

cgPath A property that holds the path as a Core Graphics Path (CGPath).

currentPoint Returns the last point in the path, or nil if the path contains no points.

description A property that holds a string description of the path.

isEmpty A Boolean property that contains whether the path is empty.


Type Alias

AnimatableData The type defining the data to animate.

Body The type of view representing the body of this view.


Initializer

init() Creates an empty path.

init(_:) Creates a path from a closure.

init(_:) Creates a path from a CGRect.

init(_:) Creates a path from a CGPath.

init(_:) Creates a path from a string.

init(_:) Creates a path from a CGMutablePath.

init(ellipseIn:) Creates a path as an ellipse defined by a given rectangle.

init(roundedRect:cornerRadius:style:) Creates a path as a rounded rectangle from a CGRect and a corner radius.

init(roundedRect:cornerSize:style:) Creates a path as a rounded rectangle from a CGRect and a corner size.


Enumeration

Element An element of a path.


Instance Method

addArc(center:radius:startAngle:endAngle:clockwise:transform:) Adds an arc of a circle to the path, specified with a radius and angles.

addArc(tangent1End:tangent2End:radius:transform:) Adds an arc of a circle to the path, specified with a radius and two tangent lines.

addCurve(to:control1:control2:) Adds a cubic Bézier curve to the path, with the specified end point and control points.

addEllipse(in:transform:) Adds an ellipse to the path.

addLine(to:) Appends a straight line segment from the current point to the specified point.

addLines(_:) Adds a sequence of connected straight-line segments to the path.

addPath(_:transform:) Appends a copy of the given path to this path.

addQuadCurve(to:control:) Adds a quadratic Bézier curve to the path, with the specified end point and control point.

addRect(_:transform:) Adds a rectangular subpath to the path.

addRects(_:transform:) Adds a sequence of rectangular subpaths to the path.

addRelativeArc(center:radius:startAngle:delta:transform:) Adds an arc of a circle to the path, specified with a radius and a difference in angle.

addRoundedRect(in:cornerSize:style:transform:) Adds a rounded rectangle to the path.

applying(_:) Returns a path constructed by applying the transform to all points of the path.

closeSubpath() Closes and completes the current subpath.

contains(_:eoFill:) Returns true if the path contains a specified point.

forEach(_:) Calls a function for every element in the path.

move(to:) Begins a new subpath at the specified point.

offsetBy(dx:dy:) Returns a path constructed by translating self by (dx, dy).

path(in:) Describes the path as a path within a rectangular frame of reference.

strokedPath(_:) Sets the style for a path's stroke.

trimmedPath(from:to:) Trims a path along a given 0 to 1 percentage range.


Type Method

==(a:b:) Returns a Boolean value indicating whether two values are equal.