IOS中CGAffineTransform的使用方法

时间:2022-03-04 16:53:38


常用:

#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)



CoreGraphics 的 仿射变换 可以用于 平移、旋转、缩放变换路径 或者图形上下文。

   (1)平移变换将路径或图形上下文中的形状的当前位置平移到另一个相对位置。举例来说,如果你在(10,20)的位置处画一个点,对它应用(30,40)的平移变换,然后绘制它,这个点将被绘制在(40,60)的位置处。为了创建一个平移变换,使用CGAffineTransformMakeTranslation函数,它将返回一个CGAffineTransform类型的仿射变换,这个函数的两个参数指定x和y方向上以点为单位的平移量。我们还可以使用CGContextTranslateCTM过程对图形上下文应用变换。


 平移变换路径

01 //平移变换
02  
03 -(void)drawRect:(CGRect)rect
04 {
05     CGMutablePathRef path = CGPathCreateMutable();
06     CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 200.0f);
07      
08     CGAffineTransform transform = CGAffineTransformMakeTranslation(100.0f, 0.0f);
09  
10  
11     CGContextRef currentContext = UIGraphicsGetCurrentContext();
12     CGPathAddRect(path, &transform, rectangle);
13     CGContextAddPath(currentContext, path);
14     [[UIColor brownColor] setStroke];
15     [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
16     CGContextSetLineWidth(currentContext, 5.0f);
17     CGContextDrawPath(currentContext, kCGPathFillStroke);
18     CGPathRelease(path);
19 }

平移变换图形上下文

01 -(void)drawRect:(CGRect)rect
02 {
03     CGMutablePathRef path = CGPathCreateMutable();
04     CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f);
05     CGPathAddRect(path, NULL, rectangle);
06      
07     CGContextRef currentContext = UIGraphicsGetCurrentContext();
08     CGContextSaveGState(currentContext);
09      
10     CGContextTranslateCTM(currentContext, 100.0f, 40.0f);
11  
12     CGContextAddPath(currentContext, path);
13     [[UIColor colorWithRed:0.20f green:0.6f blue:0.8f alpha:1.0f] setFill];
14     [[UIColor brownColor] setStroke];
15      
16     CGContextSetLineWidth(currentContext, 5.0f);
17     CGContextDrawPath(currentContext, kCGPathFillStroke);
18      
19     CGPathRelease(path);
20      
21     CGContextRestoreGState(currentContext);
22      
23      
24      
25 }

    (2)缩放是另外一个你可以使用的变换。你可以很容易地让CoreGraphics 对形状进行缩放,例如一个圆形缩放到原来的100倍。要创建一个仿射缩放变换,使用CGAffineTransformMakeScale函数,它返回一个CGAffineTransform 类型的变换对象。如果你想直接对一个图形上下文使用缩放变换,使用CGContextScaleCTM过程来缩放当前变换矩阵。

 缩放路径

01 -(void)drawRect:(CGRect)rect
02 {
03     CGMutablePathRef path = CGPathCreateMutable();
04     CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 200.0f);
05      
06  
07     CGAffineTransform transform = CGAffineTransformMakeScale(0.5f, 0.5f);
08  
09     CGContextRef currentContext = UIGraphicsGetCurrentContext();
10     CGPathAddRect(path, &transform, rectangle);
11     CGContextAddPath(currentContext, path);
12     [[UIColor brownColor] setStroke];
13     [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
14     CGContextSetLineWidth(currentContext, 5.0f);
15     CGContextDrawPath(currentContext, kCGPathFillStroke);
16     CGPathRelease(path);
17 }
  缩放图形上下文
01 -(void)drawRect:(CGRect)rect
02 {
03     CGMutablePathRef path = CGPathCreateMutable();
04     CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f);
05     CGPathAddRect(path, NULL, rectangle);
06      
07     CGContextRef currentContext = UIGraphicsGetCurrentContext();
08     CGContextSaveGState(currentContext);
09      
10  
11     CGContextScaleCTM(currentContext, 0.5f, 0.5f);
12  
13     CGContextAddPath(currentContext, path);
14     [[UIColor colorWithRed:0.20f green:0.6f blue:0.8f alpha:1.0f] setFill];
15     [[UIColor brownColor] setStroke];
16      
17     CGContextSetLineWidth(currentContext, 5.0f);
18     CGContextDrawPath(currentContext, kCGPathFillStroke);
19      
20     CGPathRelease(path);
21      
22     CGContextRestoreGState(currentContext);
23      
24      
25      
26 }

  (3)就像缩放和平移,你可以对绘制在路径上的形状和图形上下文应用旋转变换。你可以使用CGAffineTransformMakeRoation函数和一个旋转的弧度值来获取一个CGAffineTransform类型的变换.如果你想对整个图形上下文旋转指定角度,可以使用CGContextRotateCTM过程。

  旋转路径

01 -(void)drawRect:(CGRect)rect
02 {
03     CGMutablePathRef path = CGPathCreateMutable();
04     CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 200.0f);
05      
06     CGAffineTransform transform = CGAffineTransformMakeRotation((45.0f * M_PI) / 180.0f);
07     CGContextRef currentContext = UIGraphicsGetCurrentContext();
08     CGPathAddRect(path, &transform, rectangle);
09     CGContextAddPath(currentContext, path);
10     [[UIColor brownColor] setStroke];
11     [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
12     CGContextSetLineWidth(currentContext, 5.0f);
13     CGContextDrawPath(currentContext, kCGPathFillStroke);
14     CGPathRelease(path);
15 }
  旋转图形上下文
01 -(void)drawRect:(CGRect)rect
02 {
03     CGMutablePathRef path = CGPathCreateMutable();
04     CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f);
05     CGPathAddRect(path, NULL, rectangle);
06      
07     CGContextRef currentContext = UIGraphicsGetCurrentContext();
08     CGContextSaveGState(currentContext);
09     CGContextRotateCTM(currentContext, (45.0f * M_PI) / 180.0f);
10     CGContextAddPath(currentContext, path);
11     [[UIColor colorWithRed:0.20f green:0.6f blue:0.8f alpha:1.0f] setFill];
12     [[UIColor brownColor] setStroke];
13      
14     CGContextSetLineWidth(currentContext, 5.0f);
15     CGContextDrawPath(currentContext, kCGPathFillStroke);
16      
17     CGPathRelease(path);
18      
19     CGContextRestoreGState(currentContext);
20      
21      
22      
23 }
    另外我们还可以组合变换效果,使用CGAffineTransformConcact函数组合两个变换效果,这个函数的两个参数都是类型为CGAffineTransform类型的变换。

  组合多个变换效果,同时进行平移和缩放

01 -(void)drawRect:(CGRect)rect
02 {
03     CGMutablePathRef path = CGPathCreateMutable();
04     CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 200.0f);
05      
06     CGAffineTransform transform1 = CGAffineTransformMakeTranslation(100.0f, 0.0f);
07     CGAffineTransform transform2 = CGAffineTransformMakeScale(0.5f, 0.5f);
08     CGAffineTransform transform = CGAffineTransformConcat(transform1, transform2);
09     CGContextRef currentContext = UIGraphicsGetCurrentContext();
10     CGPathAddRect(path, &transform, rectangle);
11     CGContextAddPath(currentContext, path);
12     [[UIColor brownColor] setStroke];
13     [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
14     CGContextSetLineWidth(currentContext, 5.0f);
15     CGContextDrawPath(currentContext, kCGPathFillStroke);
16     CGPathRelease(path);
17 }