【iOS】CGAffineTransform和CATransform3D

学习文章

  • CATransform3D vs. CGAffineTransform?

解释

CGAffineTransform 是用于2D层面的,操作 NSView, UIView或者其他 2D Core Graphics 元素的.

CATransform3D 是 Core Animation的结构体,是用来做更复杂的关于 CALayer 的3D操作.CATransform3D 有着与 OpenGL 模型视图矩阵相同的内部结构,原因在于 Core Animation 是建立在 OpenGL 之上的, CALayer是 OpenGL 结构的一种封装.

【iOS】CGAffineTransform和CATransform3D_第1张图片
CoreAnimation.png

CGAffineTransform

struct CGAffineTransform {
  CGFloat a, b, c, d;
  CGFloat tx, ty;
};  
创建CGAffineTransform的API
/* The identity transform: [ 1 0 0 1 0 0 ]. */  
const CGAffineTransform CGAffineTransformIdentity    

/* Return the transform [ a b c d tx ty ]. */
CGAffineTransform CGAffineTransformMake(CGFloat a, CGFloat b,
  CGFloat c, CGFloat d, CGFloat tx, CGFloat ty)  
  
生成由Identity Transform发生相应转变的CGAffineTransform的API
/* Return a transform which translates by `(tx, ty)':
     t' = [ 1 0 0 1 tx ty ] */
CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx,
  CGFloat ty)   
   
/* Return a transform which scales by `(sx, sy)':
     t' = [ sx 0 0 sy 0 0 ] */ 
CGAffineTransform CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)   
   

/* Return a transform which rotates by `angle' radians:
     t' = [ cos(angle) sin(angle) -sin(angle) cos(angle) 0 0 ] */ 
CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle)
指定CGAffineTransform转变的API
/* Translate `t' by `(tx, ty)' and return the result:
     t' = [ 1 0 0 1 tx ty ] * t */
CGAffineTransform CGAffineTransformTranslate(CGAffineTransform t,
  CGFloat tx, CGFloat ty)    
  
/* Scale `t' by `(sx, sy)' and return the result:
     t' = [ sx 0 0 sy 0 0 ] * t */  
CGAffineTransform CGAffineTransformScale(CGAffineTransform t,
  CGFloat sx, CGFloat sy)
   
/* Rotate `t' by `angle' radians and return the result:
     t' =  [ cos(angle) sin(angle) -sin(angle) cos(angle) 0 0 ] * t */   
CGAffineTransform CGAffineTransformRotate(CGAffineTransform t,
  CGFloat angle)
判断CGAffineTransform的API
/* Return true if `t' is the identity transform, false otherwise. */
bool CGAffineTransformIsIdentity(CGAffineTransform t)    
    
/* Return true if `t1' and `t2' are equal, false otherwise. */
bool CGAffineTransformEqualToTransform(CGAffineTransform t1,
  CGAffineTransform t2)  

UIView的transform属性就是一个CGAffineTransform类型的数据,默认值为CGAffineTransformIdentity。我们可以利用UIView动画让这些转变变得生动.

CATransform3D

struct CATransform3D
{
  CGFloat m11, m12, m13, m14;
  CGFloat m21, m22, m23, m24;
  CGFloat m31, m32, m33, m34;
  CGFloat m41, m42, m43, m44;
};
创建CATransform3D的API
/* The identity transform: [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]. */  
const CATransform3D CATransform3DIdentity  
  
  
  
  
生成由Identity Transform发生相应转变的CATransform3D的API
/* Returns a transform that translates by '(tx, ty, tz)':
 * t' =  [1 0 0 0; 0 1 0 0; 0 0 1 0; tx ty tz 1]. */  
CATransform3D CATransform3DMakeTranslation (CGFloat tx,
    CGFloat ty, CGFloat tz)  
    
/* Returns a transform that scales by `(sx, sy, sz)':
 * t' = [sx 0 0 0; 0 sy 0 0; 0 0 sz 0; 0 0 0 1]. */
CATransform3D CATransform3DMakeScale (CGFloat sx, CGFloat sy,
    CGFloat sz)  
   
/* Returns a transform that rotates by 'angle' radians about the vector
 * '(x, y, z)'. If the vector has length zero the identity transform is
 * returned. */ 
CATransform3D CATransform3DMakeRotation (CGFloat angle, CGFloat x,
    CGFloat y, CGFloat z)
指定CATransform3D转变的API
/* Translate 't' by '(tx, ty, tz)' and return the result:
 * t' = translate(tx, ty, tz) * t. */ 
CATransform3D CATransform3DTranslate (CATransform3D t, CGFloat tx,
    CGFloat ty, CGFloat tz)    
   
/* Scale 't' by '(sx, sy, sz)' and return the result:
 * t' = scale(sx, sy, sz) * t. */ 
CATransform3D CATransform3DScale (CATransform3D t, CGFloat sx,
    CGFloat sy, CGFloat sz)  
   
/* Rotate 't' by 'angle' radians about the vector '(x, y, z)' and return
 * the result. If the vector has zero length the behavior is undefined:
 * t' = rotation(angle, x, y, z) * t. */ 
CATransform3D CATransform3DRotate (CATransform3D t, CGFloat angle,
    CGFloat x, CGFloat y, CGFloat z)
  
判断CATransform3D的API
/* Returns true if 't' is the identity transform. */ 
bool CATransform3DIsIdentity (CATransform3D t)  
   
/* Returns true if 'a' is exactly equal to 'b'. */ 
bool CATransform3DEqualToTransform (CATransform3D a,
    CATransform3D b)  
    
/* Returns true if 't' can be represented exactly by an affine transform. */
bool CATransform3DIsAffine (CATransform3D t)
与CGAffineTransform相互转换的API
/* Return a transform with the same effect as affine transform 'm'. */
CATransform3D CATransform3DMakeAffineTransform (CGAffineTransform m)  
  
/* Returns the affine transform represented by 't'. If 't' can not be
 * represented exactly by an affine transform the returned value is
 * undefined. */  
CGAffineTransform CATransform3DGetAffineTransform (CATransform3D t)  
  

CALayer的transform属性是是个CATransform3D类型的数据,默认值为CATransform3DIdentity。需要注意的是,CALayer是有隐式动画的,如果你想关掉隐式动画,用

[CATransaction setDisableActions:YES];  

另外,要特地说明一下矩阵中的一个参数m34,m34影响透视效果.当然,z方向上得有变化才会有透视效果,数值越大,透视效果越明显.正值/负值都有意义,导致透视方向的不同.

你可能感兴趣的:(【iOS】CGAffineTransform和CATransform3D)