仿射变换本质是一种矩阵变换,可以用来做平移,缩放,旋转等操作
这些操作我们可以包装到动画中去
1.apple的官方文档定义:
CGAffineTransform CGAffineTransformMake ( CGFloat a, CGFloat b, CGFloat c, CGFloat d, CGFloat tx, CGFloat ty );
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.
(a,b,c,d,tx,ty)是一个transform的构成,包含下面的这6个数
tx,ty实际上在视图中是视图的位置,我们可以改变之来做平移操作
对应的矩阵表视图如下:
2.创建transform的几种方式
通过
CGAffineTransformMake (原生矩阵)
CGAffineTransformMakeRotation (旋转)
CGAffineTransformMakeScale(缩放)
CGAffineTransformMakeTranslation(平移)
这几个方法可以创建一个 仿射变换
3.改变transform的几种方式通过
(1)在原来的transform基础上再进行一下平移操作,返回值为新的transform
CGAffineTransformTranslate ( CGAffineTransform t, CGFloat tx, CGFloat ty );
Parameters
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.
Return Value
A new affine transformation matrix.
(2)在原来的基础上进行缩放,返回一个新的transform
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.
(3)在原来的基础上旋转,注意旋转的角度是弧度值
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.
(4)反转之前的transform(逆操作)
CGAffineTransformInvert ( CGAffineTransform t );
Parameters
t
An existing affine transform.
Return Value
A new affine transformation matrix. If the affine transform passed in parameter t cannot be inverted, Quartz returns the affine transform unchanged.
(5)合并两个transform,把两个transform合并在一起
CGAffineTransformConcat ( CGAffineTransform t1, CGAffineTransform t2 );
Parameters
t1
The first affine transform.
t2
The second affine transform. This affine transform is concatenated to the first affine transform.
Return Value
A new affine transformation matrix. That is, t’ = t1*t2.
4.应用transform,例如CGPoint,CGSize,CGRect可以直接应用这个transform来进行相应的矩阵操作
(1)CGPointApplyAffineTransform (CGPoint point,CGAffineTransform t );(可以获得仿射变换之后的点..下类似)
Parameters
point
A point that specifies the x- and y-coordinates to transform.
t
The affine transform to apply.
Return Value
A new point resulting from applying the specified affine transform to the existing point.
(2)CGSizeApplyAffineTransform (CGSize size,CGAffineTransform t );
Parameters
size
A size that specifies the height and width to transform.
t
The affine transform to apply.
Return Value
A new size resulting from applying the specified affine transform to the existing size.
(3)CGRectApplyAffineTransform ( CGRect rect,CGAffineTransform t );
Parameters
rect
The rectangle whose corner points you want to transform.
t
The affine transform to apply to the rect parameter.
Return Value
The transformed rectangle.
示例,把一个点进行平移运算,得到一个新的点
CGPoint pp = CGPointMake(100, 100);
CGAffineTransform pingyi = CGAffineTransformMakeTranslation(100, 0);
CGPoint newPP = CGPointApplyAffineTransform(pp, pingyi);//对一个点做仿射变换
NSLog(@"平移之后的点 坐标是 %@", NSStringFromCGPoint(newPP));
5.评判transform
bool CGAffineTransformIsIdentity ( CGAffineTransform t );
Parameters
t
The affine transform to check.
Return Value
Returns true if t is the identity transform, false otherwise.
(1)检测一个Transform是不是恒等变换,也就是说不变
bool CGAffineTransformEqualToTransform ( CGAffineTransform t1, CGAffineTransform t2 );
Parameters
t1
An affine transform.
t2
An affine transform.
Return Value
Returns true if t1 and t2 are equal, false otherwise.
(2)比较两个transform是否相等
6.transform动画
设置号一个transform之后剩下的事就是加入到UIView动画里面去执行了
CGAffineTransform tt = CGAffineTransformMakeScale(0.5, 0.5);
[UIView animateWithDuration:1 animations:^{
view1.transform = tt;
}];
我们需要使用UIView的transform属性
其他的操作类似,当然我们也可以组合这些transform()
CGAffineTransformConcat
使用上面的方法可以合并两个transform