首先说一下CGAffineTransform的常见应用场景
- 与CoreGraphics一起用来画画;
- 根据用户的手势调整相应视图的大小、缩放、旋转等;
- 做动画的时候有用到;
反射矩阵原理
这样可以看出其实变换之后的x' 和a、c、tx有关系,y'和b、d、ty有关系。
可以通过这些值的变化组合成可以缩放、平移、旋转等各种变形的变化
简单点去想,如果c和b都为0,这样就x'和y'就只能是x和y的缩放和平移了
这样就只能通过改变a和d做缩放变换,利用tx,ty做平移变换
再简单一点,如果a=1,c=0,tx=0;b=0,d=0,ty=0;则会出现x'=x;y'=y;也就是说[1,0,0,1,0,0]这就是没有任何变换的矩阵。苹果用一个常量CGAffineTransformIdentity定义
/* The identity transform: [ 1 0 0 1 0 0 ]. */
CG_EXTERN const CGAffineTransform CGAffineTransformIdentity
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
只要是看得到的界面基本都有transform属性,并且默认是CGAffineTransformIdentity
@property(nonatomic) CGAffineTransform transform; // default is CGAffineTransformIdentity
苹果对一些常用的变换进行了整合,是开发起来更加方便
最基本的方法
这一个是最基本的方法,也是最强大的方法,对应上边分析的各个参数,但是在开发中不常用,对应的值不好掌握。苹果有提供了特定功能的方法。
/* Return the transform [ a b c d tx ty ]. */
CG_EXTERN CGAffineTransform CGAffineTransformMake(CGFloat a, CGFloat b,
CGFloat c, CGFloat d, CGFloat tx, CGFloat ty)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
平移变换矩阵
/* Return a transform which translates by `(tx, ty)':
t' = [ 1 0 0 1 tx ty ] */
CG_EXTERN CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx,
CGFloat ty) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
/* Translate
t' by
(tx, ty)' and return the result:
t' = [ 1 0 0 1 tx ty ] * t */
CG_EXTERN CGAffineTransform CGAffineTransformTranslate(CGAffineTransform t,
CGFloat tx, CGFloat ty) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
缩放变换矩阵
/* Return a transform which scales by `(sx, sy)':
t' = [ sx 0 0 sy 0 0 ] */
CG_EXTERN CGAffineTransform CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
/* Scale
t' by
(sx, sy)' and return the result:
t' = [ sx 0 0 sy 0 0 ] * t */
CG_EXTERN CGAffineTransform CGAffineTransformScale(CGAffineTransform t,
CGFloat sx, CGFloat sy) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
旋转变换矩阵
/* Return a transform which rotates by `angle' radians:
t' = [ cos(angle) sin(angle) -sin(angle) cos(angle) 0 0 ] */
CG_EXTERN CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
/* Rotate
t' by
angle' radians and return the result:
t' = [ cos(angle) sin(angle) -sin(angle) cos(angle) 0 0 ] * t */
CG_EXTERN CGAffineTransform CGAffineTransformRotate(CGAffineTransform t,
CGFloat angle) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
反转
/* Invert
t' and return the result. If
t' has zero determinant, then `t'
is returned unchanged. */
CG_EXTERN CGAffineTransform CGAffineTransformInvert(CGAffineTransform t)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
合并
/* Concatenate
t2' to
t1' and return the result:
t' = t1 * t2 */
CG_EXTERN CGAffineTransform CGAffineTransformConcat(CGAffineTransform t1,
CGAffineTransform t2) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
两个比较方法
是否是初始矩阵
/* Return true if `t' is the identity transform, false otherwise. */
CG_EXTERN bool CGAffineTransformIsIdentity(CGAffineTransform t)
CG_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
是否相同
/* Return true if
t1' and
t2' are equal, false otherwise. */
CG_EXTERN bool CGAffineTransformEqualToTransform(CGAffineTransform t1,
CGAffineTransform t2) CG_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
通过AffineTransform计算对应的的point、size、rect
point转换
/* Transform
point' by
t' and return the result:
p' = p * t
where p = [ x y 1 ]. */
CG_EXTERN CGPoint CGPointApplyAffineTransform(CGPoint point,
CGAffineTransform t) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
size 转换
/* Transform
size' by
t' and return the result:
s' = s * t
where s = [ width height 0 ]. */
CG_EXTERN CGSize CGSizeApplyAffineTransform(CGSize size, CGAffineTransform t)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
rect转换
/* Transform
rect' by
t' and return the result. Since affine transforms do
not preserve rectangles in general, this function returns the smallest
rectangle which contains the transformed corner points ofrect'. If
t'
consists solely of scales, flips and translations, then the returned
rectangle coincides with the rectangle constructed from the four
transformed corners. */
CG_EXTERN CGRect CGRectApplyAffineTransform(CGRect rect, CGAffineTransform t)
CG_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
总结
CGAffineTransform 是转换坐标的一个工具类,内部基于了一个数学公式。