An affine transformation matrix for use in drawing 2D graphics.
- iOS 2.0+
- macOS 10.0+
- tvOS 9.0+
- watchOS 2.0+
- Core Graphics
An affine transformation matrix is used to rotate, scale, translate, or skew the objects you draw in a graphics context. The
CGAffine type provides functions for creating, concatenating, and applying affine transformations.
Affine transforms are represented by a 3 by 3 matrix:
Because the third column is always
CGAffine data structure contains values for only the first two columns.
Conceptually, an affine transform multiplies a row vector representing each point (x,y) in your drawing by this matrix, producing a vector that represents the corresponding point (x’,y’):
Given the 3 by 3 matrix, the following equations are used to transform a point (x, y) in one coordinate system into a resultant point (x’,y’) in another coordinate system.
The matrix thereby “links” two coordinate systems—it specifies how points in one coordinate system map to points in another.
Note that you do not typically need to create affine transforms directly. If you want only to draw an object that is scaled or rotated, for example, it is not necessary to construct an affine transform to do so. The most direct way to manipulate your drawing—whether by movement, scaling, or rotation—is to call the functions
rotate(by:), respectively. You should generally only create an affine transform if you want to reuse it later.