Class

UIBezierPath

The UIBezierPath class lets you define a path consisting of straight and curved line segments and render that path in your custom views. You use this class initially to specify just the geometry for your path. Paths can define simple shapes such as rectangles, ovals, and arcs or they can define complex polygons that incorporate a mixture of straight and curved line segments. After defining the shape, you can use additional methods of this class to render the path in the current drawing context.

Overview

A UIBezierPath object combines the geometry of a path with attributes that describe the path during rendering. You set the geometry and attributes separately and can change them independent of one another. Once you have the object configured the way you want it, you can tell it to draw itself in the current context. Because the creation, configuration, and rendering process are all distinct steps, Bezier path objects can be reused easily in your code. You can even use the same object to render the same shape multiple times, perhaps changing the rendering options between successive drawing calls.

You set the geometry of a path by manipulating the path’s current point. When you create a new empty path object, the current point is undefined and must be set explicitly. To move the current point without drawing a segment, you use the move(to:) method. All other methods result in the addition of either a line or curve segments to the path. The methods for adding new segments always assume you are starting at the current point and ending at some new point that you specify. After adding the segment, the end point of the new segment automatically becomes the current point.

A single Bezier path object can contain any number of open or closed subpaths, where each subpath represents a connected series of path segments. Calling the close() method closes a subpath by adding a straight line segment from the current point to the first point in the subpath. Calling the move(to:) method ends the current subpath (without closing it) and sets the starting point of the next subpath. The subpaths of a Bezier path object share the same drawing attributes and must be manipulated as a group. To draw subpaths with different attributes, you must put each subpath in its own UIBezierPath object.

After configuring the geometry and attributes of a Bezier path, you draw the path in the current graphics context using the stroke() and fill() methods. The stroke() method traces the outline of the path using the current stroke color and the attributes of the Bezier path object. Similarly, the fill() method fills in the area enclosed by the path using the current fill color. (You set the stroke and fill color using the UIColor class.)

In addition to using a Bezier path object to draw shapes, you can also use it to define a new clipping region. The addClip() method intersects the shape represented by the path object with the current clipping region of the graphics context. During subsequent drawing, only content that lies within the new intersection region is actually rendered to the graphics context.

Symbols

Creating a UIBezierPath Object

init(rect: CGRect)

Creates and returns a new UIBezierPath object initialized with a rectangular path.

init(ovalIn: CGRect)

Creates and returns a new UIBezierPath object initialized with an oval path inscribed in the specified rectangle

init(roundedRect: CGRect, cornerRadius: CGFloat)

Creates and returns a new UIBezierPath object initialized with a rounded rectangular path.

init(roundedRect: CGRect, byRoundingCorners: UIRectCorner, cornerRadii: CGSize)

Creates and returns a new UIBezierPath object initialized with a rounded rectangular path.

init(arcCenter: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool)

Creates and returns a new UIBezierPath object initialized with an arc of a circle.

init(cgPath: CGPath)

Creates and returns a new UIBezierPath object initialized with the contents of a Core Graphics path.

func reversing()

Creates and returns a new bezier path object with the reversed contents of the current path.

Constructing a Path

func move(to: CGPoint)

Moves the receiver’s current point to the specified location.

func addLine(to: CGPoint)

Appends a straight line to the receiver’s path.

func addCurve(to: CGPoint, controlPoint1: CGPoint, controlPoint2: CGPoint)

Appends a cubic Bézier curve to the receiver’s path.

func addQuadCurve(to: CGPoint, controlPoint: CGPoint)

Appends a quadratic Bézier curve to the receiver’s path.

func close()

Closes the most recently added subpath.

func removeAllPoints()

Removes all points from the receiver, effectively deleting all subpaths.

func append(UIBezierPath)

Appends the contents of the specified path object to the receiver’s path.

var cgPath: CGPath

The Core Graphics representation of the path.

var currentPoint: CGPoint

The current point in the graphics path.

Accessing Drawing Properties

var lineWidth: CGFloat

The line width of the path.

var lineCapStyle: CGLineCap

The shape of the paths end points when stroked.

var lineJoinStyle: CGLineJoin

The shape of the joints between connected segments of a stroked path.

var miterLimit: CGFloat

The limiting value that helps avoid spikes at junctions between connected line segments.

var flatness: CGFloat

The factor that determines the rendering accuracy for curved path segments.

var usesEvenOddFillRule: Bool

A Boolean indicating whether the even-odd winding rule is in use for drawing paths.

Drawing Paths

func fill()

Paints the region enclosed by the receiver’s path using the current drawing properties.

func fill(with: CGBlendMode, alpha: CGFloat)

Paints the region enclosed by the receiver’s path using the specified blend mode and transparency values.

func stroke()

Draws a line along the receiver’s path using the current drawing properties.

func stroke(with: CGBlendMode, alpha: CGFloat)

Draws a line along the receiver’s path using the specified blend mode and transparency values.

Clipping Paths

func addClip()

Intersects the area enclosed by the receiver’s path with the clipping path of the current graphics context and makes the resulting shape the current clipping path.

Hit Detection

func contains(CGPoint)

Returns a Boolean value indicating whether the area enclosed by the receiver contains the specified point.

var isEmpty: Bool

A Boolean value indicating whether the path has any valid elements.

var bounds: CGRect

The bounding rectangle of the path.

Applying Transformations

func apply(CGAffineTransform)

Transforms all points in the path using the specified affine transform matrix.

Constants

UIRectCorner

The corners of a rectangle.

Relationships

Inherits From