NSAffineTransform
The NSAffineTransform
class provides methods for creating, concatenating, and applying affine transformations.
A transformation specifies how points in one coordinate system are transformed to points in another coordinate system. An affine transformation is a special type of transformation that preserves parallel lines in a path but does not necessarily preserve lengths or angles. Scaling, rotation, and translation are the most commonly used manipulations supported by affine transforms, but shearing is also possible.
Methods for applying affine transformations to the current graphics context and a method for applying an affine transformation to an NSBezierPath
object are described in NSAffineTransform Additions Reference in the Application Kit.

Creates and returns a new
NSAffineTransform
object initialized to the identity matrix.Declaration
ObjectiveC
+ (NSAffineTransform *)transform
Return Value
A new identity transform object. This matrix transforms any point to the same point.
Availability
Available in OS X v10.0 and later.
See Also

Initializes the receiver’s matrix using another transform object and returns the receiver.
Declaration
Swift
convenience init(transform
transform
: NSAffineTransform)ObjectiveC
 (instancetype)initWithTransform:(NSAffineTransform *)
aTransform
Parameters
aTransform
The transform object whose matrix values should be copied to this object.
Return Value
A new transform object initialized with the matrix values of
aTransform
.Availability
Available in OS X v10.0 and later.
See Also

Applies a rotation factor (measured in degrees) to the receiver’s transformation matrix.
Declaration
Swift
func rotateByDegrees(_
angle
: CGFloat)ObjectiveC
 (void)rotateByDegrees:(CGFloat)
angle
Parameters
angle
The rotation angle, measured in degrees.
Discussion
After invoking this method, applying the receiver’s matrix turns the axes counterclockwise about the current origin by
angle
degrees, in addition to performing all previous transformations.Availability
Available in OS X v10.0 and later.

Applies a rotation factor (measured in radians) to the receiver’s transformation matrix.
Declaration
Swift
func rotateByRadians(_
angle
: CGFloat)ObjectiveC
 (void)rotateByRadians:(CGFloat)
angle
Parameters
angle
The rotation angle, measured in radians.
Discussion
After invoking this method, applying the receiver’s matrix turns the axes counterclockwise about the current origin by
angle
radians, in addition to performing all previous transformations.Availability
Available in OS X v10.0 and later.

Applies the specified scaling factor along both x and y axes to the receiver’s transformation matrix.
Parameters
scale
The scaling factor to apply to both axes. Specifying a negative value has the effect of inverting the direction of the axes in addition to scaling them. A scaling factor of 1.0 scales the content to exactly the same size.
Discussion
After invoking this method, applying the receiver’s matrix modifies the unit lengths along the current x and y axes by a factor of
scale
, in addition to performing all previous transformations.Availability
Available in OS X v10.0 and later.

Applies scaling factors to each axis of the receiver’s transformation matrix.
Declaration
Swift
func scaleXBy(_
scaleX
: CGFloat, yByscaleY
: CGFloat)Parameters
scaleX
The scaling factor to apply to the x axis.
scaleY
The scaling factor to apply to the y axis.
Discussion
After invoking this method, applying the receiver’s matrix modifies the unit length on the x axis by a factor of
scaleX
and the y axis by a factor ofscaleY
, in addition to performing all previous transformations. A value of 1.0 for either axis scales the content on that axis to the same size.Availability
Available in OS X v10.0 and later.

Applies the specified translation factors to the receiver’s transformation matrix.
Declaration
Swift
func translateXBy(_
deltaX
: CGFloat, yBydeltaY
: CGFloat)Parameters
deltaX
The number of units to move along the x axis.
deltaY
The number of units to move along the y axis.
Discussion
Subsequent transformations cause coordinates to be shifted by
deltaX
units along the x axis and bydeltaY
units along the y axis. Translation factors do not affectNSSize
values, which specify a differential between points.Availability
Available in OS X v10.0 and later.

Appends the specified matrix to the receiver’s matrix.
Declaration
Swift
func appendTransform(_
transform
: NSAffineTransform)ObjectiveC
 (void)appendTransform:(NSAffineTransform *)
aTransform
Parameters
aTransform
The matrix to append to the receiver.
Discussion
This method multiplies the receiver's matrix by the matrix in
aTransform
and replaces the receiver's matrix with the results. This type of operation is the same as applying the transformations in the receiver followed by the transformations inaTransform
.Availability
Available in OS X v10.0 and later.
See Also

Prepends the specified matrix to the receiver’s matrix.
Declaration
Swift
func prependTransform(_
transform
: NSAffineTransform)ObjectiveC
 (void)prependTransform:(NSAffineTransform *)
aTransform
Parameters
aTransform
The matrix to prepend to the receiver.
Discussion
This method multiplies the matrix in
aTransform
by the receiver’s matrix and replaces the receiver’s matrix with the result. This type of operation is the same as applying the transformations inaTransform
followed by the transformations in the receiver.Availability
Available in OS X v10.0 and later.
See Also

Replaces the receiver’s matrix with its inverse matrix.
Declaration
Swift
func invert()
ObjectiveC
 (void)invert
Discussion
Inverse matrices are useful for undoing the effects of a matrix. If a previous point (x,y) was transformed to (x’,y’), inverting the matrix and applying it to point (x’,y’) yields the point (x,y).
You can also use inverse matrices in conjunction with the
concat
method to remove the effects of concatenating the matrix to the current transformation matrix of the current graphic context.Availability
Available in OS X v10.0 and later.

Applies the receiver’s transform to the specified
NSPoint
data type and returns the results.Declaration
Parameters
aPoint
The point in the current coordinate system to which you want to apply the matrix.
Return Value
The resulting point after applying the receiver's transformations.
Availability
Available in OS X v10.0 and later.
See Also

Applies the receiver’s transform to the specified
NSSize
data type and returns the results.Declaration
Parameters
aSize
The size data to which you want to apply the matrix.
Return Value
The resulting size after applying the receiver's transformations.
Discussion
This method applies the current rotation and scaling factors to
aSize
; it does not apply translation factors. You can think of this method as transforming a vector whose origin is (0, 0) and whose end point is specified by the value inaSize
. After the rotation and scaling factors are applied, this method effectively returns the end point of the new vector.This method is useful for transforming delta or distance values when you need to take scaling and rotation factors into account.
Availability
Available in OS X v10.0 and later.
See Also

transformStruct transformStruct
PropertyReturns the matrix coefficients stored in the receiver’s matrix.
Declaration
Swift
var transformStruct: NSAffineTransformStruct
ObjectiveC
@property NSAffineTransformStruct transformStruct
Discussion
The matrix is of the form shown in Transform Mathematics, and the sixelement structure defined by the
NSAffineTransformStruct
structure is of the form:{m11, m12, m21, m22, tX, tY}
The
NSAffineTransformStruct
structure is an alternate representation of a transformation matrix that can be used to specify matrix values directly.Availability
Available in OS X v10.0 and later.
See Also

Sets the current transformation matrix to the receiver’s transformation matrix.
Declaration
Swift
func set()
ObjectiveC
 (void)set
Discussion
The current transformation is stored in the current graphics context and is applied to subsequent drawing operations. You should use this method sparingly because it removes the existing transformation matrix, which is an accumulation of transformation matrices for the screen, window, and any superviews. Instead use the
concat
method to add this transformation matrix to the current transformation matrix.Import Statement
ObjectiveC
@import AppKit;
Swift
import AppKit
Availability
Available in OS X v10.0 and later.

Appends the receiver’s matrix to the current transformation matrix stored in the current graphics context, replacing the current transformation matrix with the result.
Declaration
Swift
func concat()
ObjectiveC
 (void)concat
Discussion
Concatenation is performed by matrix multiplication—see Manipulating Transform Values.
If this method is invoked from within an
NSView
drawRect:
method, then the current transformation matrix is an accumulation of the screen, window, and any superview’s transformation matrices. Invoking this method defines a new user coordinate system whose coordinates are mapped into the former coordinate system according to the receiver’s transformation matrix. To undo the concatenation, you must invert the receiver’s matrix and invoke this method again.Import Statement
ObjectiveC
@import AppKit;
Swift
import AppKit
Availability
Available in OS X v10.0 and later.

Creates and returns a new
NSBezierPath
object with each point in the given path transformed by the receiver.Declaration
Swift
func transformBezierPath(_
aPath
: NSBezierPath) > NSBezierPathObjectiveC
 (NSBezierPath *)transformBezierPath:(NSBezierPath *)
aPath
Parameters
aPath
An object representing the bezier path to be used in the transformation.
Discussion
The original
NSBezierPath
object is not modified.Import Statement
ObjectiveC
@import AppKit;
Swift
import AppKit
Availability
Available in OS X v10.0 and later.
See Also
 transformPoint:
– transformSize:
Data Types

This type defines the threebythree matrix that performs an affine transform between two coordinate systems.
Declaration
Swift
struct NSAffineTransformStruct { var m11: CGFloat var m12: CGFloat var m21: CGFloat var m22: CGFloat var tX: CGFloat var tY: CGFloat init() init(m11
m11
: CGFloat, m12m12
: CGFloat, m21m21
: CGFloat, m22m22
: CGFloat, tXtX
: CGFloat, tYtY
: CGFloat) }ObjectiveC
typedef struct _NSAffineTransformStruct { CGFloat m11, m12, m21, m22; CGFloat tX, tY; } NSAffineTransformStruct;
Discussion
For more details, see Cocoa Drawing Guide.
Import Statement
ObjectiveC
@import Foundation;
Swift
import Foundation
Availability
Available in OS X v10.0 and later.
Copyright © 2016 Apple Inc. All rights reserved. Terms of Use  Privacy Policy  Updated: 20090715