CoreGraphics中的 CGAffineTransform的讲解

1对于 CoreGraphics 框架,是绘制平面图形时所需的框架与 Foundation 框架,UIKit 框架为项目创建时默认提供的3大框架。

2  对于CGAffineTransform 的描述,apple 官方文档是这样描述的:

The CGAffineTransform data structure represents a matrix used for affine transformations. A transformation specifies how points in one coordinate system map to points in another coordinate system. An affine transformation is a special type of mapping 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 skewing is also possible.

翻译如下:CGAffineTransform这种数据结构表示了一个用于仿射变换(http://baike.baidu.com/view/954621.htm?fr=aladdin)的矩阵(http://baike.baidu.com/subview/10337/6436981.htm?fr=aladdin)。一种变换指定了坐标系中的一点的位置如何从 A 坐标系放射到 B 坐标系。仿射变换是一种特殊类型的变换,它可以保存平行线的路径但是不保存长度或角度。缩放,旋转,平移是仿射变换中最常见的操作,倾斜也是支持的。

Quartz provides functions that create, concatenate, and apply affine transformations using the CGAffineTransform data structure. For information on how to use affine transformation functions, see Quartz 2D Programming Guide.

翻译如下:Quartz提供了创建,连接的函数,应用仿射变换时使用CGAffineTransform数据结构,关于仿射变换更多的信息请参考Quartz 2D Programming Guide.

You typically do not need to create an affine transform directly—CGContext Reference describes functions that modify the current affine transform. If you don’t plan to reuse an affine transform, you may want to use CGContextScaleCTMCGContextRotateCTM,CGContextTranslateCTM, or CGContextConcatCTM.

翻译如下:你通常不需要直接创建一个仿射变换, CGContext Reference描述了如何修改当前变换的函数。如果你不打算重用这个仿射变换,你可以使用

CGContextScaleCTMCGContextRotateCTM,CGContextTranslateCTM, or CGContextConcatCTM.

3:相关说明

struct CGAffineTransform {

  CGFloat a, b, c, d;

  CGFloat tx, ty;

};首先这是一个结构体,包括6个浮点型的变量 a,b,c,d,tx,ty。其实它们是一个3行3列矩阵(matrix)的前2列的元素,最后一列默认为

(0

  0

 1)其位置描述苹果是这样描述的

Parameters
a

The value at position [1,1] in the matrix.

b

The value at position [1,2] in the matrix.

c

The value at position [2,1] in the matrix.

d

The value at position [2,2] in the matrix.

tx

The value at position [3,1] in the matrix.

ty

The value at position [3,2] in the matrix.

最终由这9个参数构成了一个3行3列的矩阵
 |   a, b,  0  |
 |   c, d,  0  |
 |  tx, ty,1    |

a: Creating an Affine Transformation Matrix(创建一个仿射变换矩阵)
  • Returns an affine transformation matrix constructed from values you provide.(根据你提供的值返回一个仿射矩阵)

    Declaration

    OBJECTIVE-C

    CGAffineTransform CGAffineTransformMake ( CGFloat a, CGFloat b, CGFloat c, CGFloat d, CGFloat tx, CGFloat ty );

    Return Value

    A new affine transform matrix constructed from the values you specify.(根据你指定的值构建一个新的仿射矩阵)

    Discussion

    This function creates a CGAffineTransform structure that represents a new affine transformation matrix, which you can use (and reuse, if you want) to transform a coordinate system. The matrix takes the following form:

    image: ../Art/equation01.pict

    Because the third column is always (0,0,1), the CGAffineTransform data structure returned by this function contains values for only the first two columns.(这个函数创建了一个CGAffineTransform结构变量,用于表示一个新的仿射变换矩阵,你可以使用它在坐标系统中变换你想改变的视图,矩阵的形式如下)


    其中b,c,d 这三种情况都以系统默认的 transform 为基准,默认的 transform 为(1,0,1,0,0,0)。即视图最初在的布局位置

b: CGAffineTransformMakeRotation (用于旋转的函数)

Returns an affine transformation matrix constructed from a rotation value you provide.

Declaration

SWIFT

func CGAffineTransformMakeRotation(_ angleCGFloat) -> CGAffineTransform

OBJECTIVE-C

CGAffineTransform CGAffineTransformMakeRotation ( CGFloat angle );

Parameters
angle

The angle, in radians, by which this matrix rotates the coordinate system axes. In iOS, a positive value specifies counterclockwise rotation and a negative value specifies clockwise rotation. In OS X, a positive value specifies clockwise rotation and a negative value specifies counterclockwise rotation.(对于参数的解释:这个角度(弧度表示),通过这个参数决定在坐标系统中旋转的程度及旋转方向,在IOS,正值指定逆时针旋转和负值指定为顺时针方向旋转。在OS X中,一个正值指定为顺时针方向旋转,而负值指定逆时针旋转。)

Return Value

A new affine transformation matrix.








This function creates a CGAffineTransform structure, which you can use (and reuse, if you want) to rotate a coordinate system. The matrix takes the following form:

image: ../Art/equation10.pict

The actual direction of rotation is dependent on the coordinate system orientation of the target platform, which is different in iOS and OS X. Because the third column is always (0,0,1), the CGAffineTransform data structure returned by this function contains values for only the first two columns.旋转的实际方向取决于目标平台的坐标系统取向,在iOS和OS X是不同的。坐标系的第三列是永远(0,0,1),这个函数返回的CGAffineTransform数据结构包含的值只有头两列。

These are the resulting equations that Quartz uses to apply the rotation to a point (x, y): (Quartz将使用如下的方程组对点(x,y)重新布局)

image: ../Art/equation11.pict  ()

c:CGAffineTransformMakeScale(用于缩放的函数)

Returns an affine transformation matrix constructed from scaling values you provide.

Declaration



















SWIFT

func CGAffineTransformMakeScale(_ sxCGFloat,
                              _ syCGFloat) -> CGAffineTransform

OBJECTIVE-C




CGAffineTransform CGAffineTransformMakeScale ( CGFloat sx, CGFloat sy );

Parameters
sx

The factor by which to scale the x-axis of the coordinate system.(sx将用于缩放坐标系 X 轴上的点)

sy

The factor by which to scale the y-axis of the coordinate system. (sy将用于缩放坐标系Y 轴上的点)

Return Value

A new affine transformation matrix.

Discussion

This function creates a CGAffineTransform structure, which you can use (and reuse, if you want) to scale a coordinate system. The matrix takes the following form:

image: ../Art/equation08.pict

Because the third column is always (0,0,1), the CGAffineTransform data structure returned by this function contains values for only the first two columns.

These are the resulting equations that Quartz uses to scale the coordinates of a point (x,y):

image: ../Art/equation09.pict



d:
CGAffineTransformMakeTranslation(用于平移的函数)

Returns an affine transformation matrix constructed from translation values you provide.

Declaration

SWIFT

func CGAffineTransformMakeTranslation(_ txCGFloat,
                                    _ tyCGFloat) -> CGAffineTransform

OBJECTIVE-C

CGAffineTransform CGAffineTransformMakeTranslation ( CGFloat tx, CGFloat ty );

Parameters
tx

The value by which to move the x-axis of the coordinate system.(这个参数决定了在 X 轴上的移动)

ty

The value by which to move the y-axis of the coordinate system.(这个参数决定了在 Y 轴上的移动)

Return Value

A new affine transform matrix.

Discussion

This function creates a CGAffineTransform structure. which you can use (and reuse, if you want) to move a coordinate system. The matrix takes the following form:

image: ../Art/equation06.pict

Because the third column is always (0,0,1), the CGAffineTransform data structure returned by this function contains values for only the first two columns.

These are the resulting equations Quartz uses to apply the translation to a point (x,y):

image: ../Art/equation07.pict

e:CGAffineTransformTranslate(根据指定的Transform再做平移变换,与 CGAffineTransformMakeTanslate 其他相同)
Declaration

SWIFT

func CGAffineTransformTranslate(_ tCGAffineTransform,
                              _ txCGFloat,
                              _ tyCGFloat) -> CGAffineTransform

OBJECTIVE-C

CGAffineTransform CGAffineTransformTranslate ( CGAffineTransform t, CGFloat tx, CGFloat ty );


t

An existing affine transform.

tx

The value by which to move x values with the affine transform.

ty

The value by which to move y values with the affine transform.


f:( 根据指定的 transform 进行缩放 )

SWIFT

func CGAffineTransformScale(_ tCGAffineTransform,
                          _ sxCGFloat,
                          _ syCGFloat) -> CGAffineTransform

OBJECTIVE-C

CGAffineTransform CGAffineTransformScale ( CGAffineTransform t, CGFloat sx, CGFloat sy );

Parameters
t

An existing affine transform.

sx

The value by which to scale x values of the affine transform.

sy

The value by which to scale y values of the affine transform.

g:(根据指定的 transform 进行旋转
Declaration

SWIFT

func CGAffineTransformRotate(_ tCGAffineTransform,
                           _ angleCGFloat) -> CGAffineTransform

OBJECTIVE-C

CGAffineTransform CGAffineTransformRotate ( CGAffineTransform t, CGFloat angle );

Parameters
t

An existing affine transform.

angle

The angle, in radians, by which to rotate the affine transform. In iOS, a positive value specifies counterclockwise rotation and a negative value specifies clockwise rotation. In OS X, a positive value specifies clockwise rotation and a negative value specifies counterclockwise rotation.

h:( 关于 CGAffineTransform的详细信息
  • A structure for holding an affine transformation matrix.

    Declaration

    OBJECTIVE-C

    struct CGAffineTransform { CGFloat a; CGFloat b; CGFloat c; CGFloat d; CGFloat tx; CGFloat ty; }; typedef structCGAffineTransform CGAffineTransform;

    Fields
    a

    The entry at position [1,1] in the matrix.

    b

    The entry at position [1,2] in the matrix.

    c

    The entry at position [2,1] in the matrix.

    d

    The entry at position [2,2] in the matrix.

    tx

    The entry at position [3,1] in the matrix.

    ty

    The entry at position [3,2] in the matrix.

    Discussion

    In Quartz 2D, an affine transformation matrix is used to rotate, scale, translate, or skew the objects you draw in a graphics context. TheCGAffineTransform type provides functions for creating, concatenating, and applying affine transformations.

    In Quartz, affine transforms are represented by a 3 by 3 matrix:

    image: ../Art/equation01.pict

    Because the third column is always (0,0,1), the CGAffineTransform data structure contains values for only the first two columns.

    Conceptually, a Quartz 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’):

    image: ../Art/equation02.pict

    Given the 3 by 3 matrix, Quartz uses the following equations to transform a point (x, y) in one coordinate system into a resultant point (x’,y’) in another coordinate system.

    image: ../Art/equation03.pict

    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 CGContextTranslateCTMCGContextScaleCTM, or CGContextRotateCTM, respectively. You should generally only create an affine transform if you want to reuse it later.


Constants

  • The identity transform.

    Declaration

    SWIFT

    let CGAffineTransformIdentity: CGAffineTransform

    OBJECTIVE-C

    constCGAffineTransformCGAffineTransformIdentity;

    Constants
    • CGAffineTransformIdentity

      The identity transform:image: ../Art/equation04.gif







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值