首页 > 代码库 > CoreGraphics 之CGAffineTransform仿射变换(3)

CoreGraphics 之CGAffineTransform仿射变换(3)

 CoreGraphics 之CGAffineTransform仿射变换(3)

 

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

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

 平移变换路径 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//平移变换
 
-(void)drawRect:(CGRect)rect
{
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 200.0f);
     
    CGAffineTransform transform = CGAffineTransformMakeTranslation(100.0f, 0.0f);
 
 
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGPathAddRect(path, &transform, rectangle);
    CGContextAddPath(currentContext, path);
    [[UIColor brownColor] setStroke];
    [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
    CGContextSetLineWidth(currentContext, 5.0f);
    CGContextDrawPath(currentContext, kCGPathFillStroke);
    CGPathRelease(path);
}

平移变换图形上下文 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-(void)drawRect:(CGRect)rect
{
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f);
    CGPathAddRect(path, NULL, rectangle);
     
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSaveGState(currentContext);
     
    CGContextTranslateCTM(currentContext, 100.0f, 40.0f);
 
    CGContextAddPath(currentContext, path);
    [[UIColor colorWithRed:0.20f green:0.6f blue:0.8f alpha:1.0f] setFill];
    [[UIColor brownColor] setStroke];
     
    CGContextSetLineWidth(currentContext, 5.0f);
    CGContextDrawPath(currentContext, kCGPathFillStroke);
     
    CGPathRelease(path);
     
    CGContextRestoreGState(currentContext);
     
     
     
}

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

 缩放路径 

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

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

  旋转路径 

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

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

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-(void)drawRect:(CGRect)rect
{
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 200.0f);
     
    CGAffineTransform transform1 = CGAffineTransformMakeTranslation(100.0f, 0.0f);
    CGAffineTransform transform2 = CGAffineTransformMakeScale(0.5f, 0.5f);
    CGAffineTransform transform = CGAffineTransformConcat(transform1, transform2);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGPathAddRect(path, &transform, rectangle);
    CGContextAddPath(currentContext, path);
    [[UIColor brownColor] setStroke];
    [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
    CGContextSetLineWidth(currentContext, 5.0f);
    CGContextDrawPath(currentContext, kCGPathFillStroke);
    CGPathRelease(path);
}

 

CoreGraphics 之CGAffineTransform仿射变换(3)