Declaration
compose().trim({
from?: number,
to?: number,
})nonisolated func trim(
from startFraction: CGFloat = 0,
to endFraction: CGFloat = 1
) -> some ShapeParameters
options.fromnumberoptions.tonumber
Return value
A shape built by capturing a portion of this shape's path.
Discussion
To create a Shape instance, you define the shape's path using lines and
curves. Use the trim(from:to:) method to draw a portion of a shape by
ignoring portions of the beginning and ending of the shape's path.
For example, if you're drawing a figure eight or infinity symbol (∞)
starting from its center, setting the startFraction and endFraction
to different values determines the parts of the overall shape.
The following example shows a simplified infinity symbol that draws only three quarters of the full shape. That is, of the two lobes of the symbol, one lobe is complete and the other is half complete.
Path { path in
path.addLines([
.init(x: 2, y: 1),
.init(x: 1, y: 0),
.init(x: 0, y: 1),
.init(x: 1, y: 2),
.init(x: 3, y: 0),
.init(x: 4, y: 1),
.init(x: 3, y: 2),
.init(x: 2, y: 1)
])
}
.trim(from: 0.25, to: 1.0)
.scale(50, anchor: .topLeading)
.stroke(Color.black, lineWidth: 3)Changing the parameters of trim(from:to:) to
.trim(from: 0, to: 1) draws the full infinity symbol, while
.trim(from: 0, to: 0.5) draws only the left lobe of the symbol.
See also
More in Shape modifiers.
- fillFills this shape with a color or gradient.
- intersectionReturns a new shape with filled regions common to both shapes.
- lineIntersectionReturns a new shape with a line from this shape that overlaps the filled regions of the given shape.
- lineSubtractionReturns a new shape with a line from this shape that does not overlap the filled region of the given shape.
- offsetChanges the relative position of this shape using the specified size.
- pathDescribes this shape as a path within a rectangular frame of reference.
- rotationRotates this shape around an anchor point at the angle you specify.
- scaleScales this shape without changing its bounding frame.
- sizeReturns a new version of self representing the same shape, but that will ask it to create its path from a rect of size. This does not affect the layout properties of any views created from the shape (e.g. by filling it).
- strokeTraces the outline of this shape with a color or gradient.
- subtractingReturns a new shape with filled regions from this shape that are not in the given shape.
- symmetricDifferenceReturns a new shape with filled regions either from this shape or the given shape, but not in both.