# CGAffineTransform有关函数

www.myexceptions.net  网友分享于：2013-06-30  浏览：41次
CGAffineTransform相关函数

CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI_2);
[xxx setTransform:rotation];

#define M_E         2.71828182845904523536028747135266250   e
#define M_LOG2E     1.44269504088896340735992468100189214   log 2e
#define M_LOG10E    0.434294481903251827651128918916605082  log 10e
#define M_LN2       0.693147180559945309417232121458176568  log e2
#define M_LN10      2.30258509299404568401799145468436421   log e10
#define M_PI        3.14159265358979323846264338327950288   pi
#define M_PI_2      1.57079632679489661923132169163975144   pi/2
#define M_PI_4      0.785398163397448309615660845819875721  pi/4
#define M_1_PI      0.318309886183790671537767526745028724  1/pi
#define M_2_PI      0.636619772367581343075535053490057448  2/pi
#define M_2_SQRTPI  1.12837916709551257389615890312154517   2/sqrt(pi)
#define M_SQRT2     1.41421356237309504880168872420969808   sqrt(2)
#define M_SQRT1_2   0.707106781186547524400844362104849039  1/sqrt(2)

from:http://donbe.blog.163.com/blog/static/138048021201061054243442/

CGAffineTransformMakeTranslation(width, 0.0);是改变位置的，
CGAffineTransformRotate(transform, M_PI);是旋转的。
CGAffineTransformMakeRotation(-M_PI);也是旋转的
transform = CGAffineTransformScale(transform, -1.0, 1.0);是缩放的。
view.transform = CGAffineTransformIdentity;线性代数里面讲的矩阵变换，这个是恒等变换

Quartz转换实现的原理:Quartz把绘图分成两个部分，
用户空间，即和设备无关，
设备空间，

Quartz提供的3大功能

void CGContextDrawImage (
CGContextRef c,
CGRect rect,
CGImageRef image
);

CGContextTranslateCTM (myContext, 100, 50);

include <math.h>
static inline double radians (double degrees) {return degrees * M_PI/180;}

CGContextScaleCTM (myContext, .5, .75);

CGContextTranslateCTM (myContext, w,h);

CGContextTranslateCTM (myContext, w/4, 0);
CGContextScaleCTM (myContext, .25,  .5);

CGContextScaleCTM (myContext, .25,  .5);
CGContextTranslateCTM (myContext, w/4, 0);

void CGContextConcatCTM (
CGContextRef c,
CGAffineTransform transform
);

Creating Affine Transforms

CGAffineTransform CGAffineTransformMakeTranslation (
CGFloat tx,
CGFloat ty
);

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

CGAffineTransform CGAffineTransformMakeRotation (
CGFloat angle
);

CGAffineTransform CGAffineTransformRotate (
CGAffineTransform t,
CGFloat angle
);

CGAffineTransform CGAffineTransformMakeScale (
CGFloat sx,
CGFloat sy
);

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

CGAffineTransform CGAffineTransformInvert (
CGAffineTransform t
);

CGRect CGRectApplyAffineTransform (
CGRect rect,
CGAffineTransform t
);

bool CGAffineTransformEqualToTransform (
CGAffineTransform t1,
CGAffineTransform t2
);

CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform (
CGContextRef c
);

CGPoint CGContextConvertPointToDeviceSpace (
CGContextRef c,
CGPoint point
);

CGPoint CGContextConvertPointToUserSpace (
CGContextRef c,
CGPoint point
);

CGSize CGContextConvertSizeToDeviceSpace (
CGContextRef c,
CGSize size
);

CGSize CGContextConvertSizeToUserSpace (
CGContextRef c,
CGSize size
);

CGRect CGContextConvertRectToDeviceSpace (
CGContextRef c,
CGRect rect
);

CGRect CGContextConvertRectToUserSpace (
CGContextRef c,
CGRect rect
);

CTM真正的数学行为

x y 是原先点的坐标

可以用函数判断这个矩阵是不是一个 identity matrix
bool CGAffineTransformIsIdentity (
CGAffineTransform t
);

- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation   duration:(NSTimeInterval)duration
{

if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
b=YES;

self.view=mainvv;
self.view.transform = CGAffineTransformIdentity;
self.view.bounds = CGRectMake(0.0, 0.0, 768.0, 1004.0);

}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
b=NO;

self.view = self.vv;
self.view.transform = CGAffineTransformIdentity;
self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 748.0);

}
else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{

b=YES;
self.view=mainvv;
self.view.transform = CGAffineTransformIdentity;
self.view.bounds = CGRectMake(0.0, 0.0, 768.0, 1004.0);

}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{

b=NO;
self.view = self.vv;
self.view.transform = CGAffineTransformIdentity;
self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 748.0);

}

}

3

Quartz转换实现的原理:Quartz把绘图分成两个部分，
用户空间，即和设备无关，
设备空间，

Quartz提供的3大功能

```void CGContextDrawImage (
CGContextRef c,
CGRect rect,
CGImageRef image
);

CGContextTranslateCTM (myContext, 100, 50);

```

`旋转函数`
```include <math.h>
static inline double radians (double degrees) {return degrees * M_PI/180;}```
```CGContextRotateCTM (myContext, radians(–45.));

CGContextScaleCTM (myContext, .5, .75);

CGContextTranslateCTM (myContext, w,h);

CGContextTranslateCTM (myContext, w/4, 0);
CGContextScaleCTM (myContext, .25,  .5);

CGContextScaleCTM (myContext, .25,  .5);
CGContextTranslateCTM (myContext, w/4, 0);

void CGContextConcatCTM (
CGContextRef c,
CGAffineTransform transform
);

Creating Affine Transforms

CGAffineTransform CGAffineTransformMakeTranslation (
CGFloat tx,
CGFloat ty
);

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

CGAffineTransform CGAffineTransformMakeRotation (
CGFloat angle
);

CGAffineTransform CGAffineTransformRotate (
CGAffineTransform t,
CGFloat angle
);

CGAffineTransform CGAffineTransformMakeScale (
CGFloat sx,
CGFloat sy
);

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

CGAffineTransform CGAffineTransformInvert (
CGAffineTransform t
);

CGRect CGRectApplyAffineTransform (
CGRect rect,
CGAffineTransform t
);

bool CGAffineTransformEqualToTransform (
CGAffineTransform t1,
CGAffineTransform t2
);

CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform (
CGContextRef c
);

CGPoint CGContextConvertPointToDeviceSpace (
CGContextRef c,
CGPoint point
);

CGPoint CGContextConvertPointToUserSpace (
CGContextRef c,
CGPoint point
);

CGSize CGContextConvertSizeToDeviceSpace (
CGContextRef c,
CGSize size
);

CGSize CGContextConvertSizeToUserSpace (
CGContextRef c,
CGSize size
);

CGRect CGContextConvertRectToDeviceSpace (
CGContextRef c,
CGRect rect
);

CGRect CGContextConvertRectToUserSpace (
CGContextRef c,
CGRect rect
);

CTM真正的数学行为

bool CGAffineTransformIsIdentity (
CGAffineTransform t
);

```