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)的更多相关文章

  1. [Xcode 实际操作]二、视图与手势-(9)CGAffineTransform仿射变换的使用

    目录:[Swift]Xcode实际操作 本文将演示使用视图对象的仿射变换功能,旋转视图对象. import UIKit class ViewController: UIViewController { ...

  2. CGAffineTransform

    这个是CoreGraphics框架中的CGAffineTransform类,可用于设定UIView的transform属性.控制视图的缩放.旋转和平移操作.另称仿射变换矩阵. Quartz转换实现原理 ...

  3. CGAffineTransform相关函数

    CoreGraphics.h CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI_2);
[xxx setTransform ...

  4. IOSView翻转扭矩位移

    CoreGraphics.h CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI_2);[xxx setTransform: ...

  5. iOS - 视图与手势(UIview & UIGestureRecognizer)

    01 UIView视图的基本使用 --- 在根视图中添加2个UIView视图 //视图确实加载时调用 - (void)viewDidLoad { [super viewDidLoad]; // Do ...

  6. iOS开发--知识点总结

    1 .全局变量,变量名前加下划线.和系统一致. 2 . nil指针为空   @“”字符串为空 (内容为空)       ==  判断内存地址   基本变量    对于一些基本类型 可以使用==来判断, ...

  7. iOS核心动画高级技巧之图层变换和专用图层(二)

    iOS核心动画高级技巧之CALayer(一) iOS核心动画高级技巧之图层变换和专用图层(二)iOS核心动画高级技巧之核心动画(三)iOS核心动画高级技巧之性能(四)iOS核心动画高级技巧之动画总结( ...

  8. [Swift]Xcode实际操作

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  9. iOS下的2D仿射变换机制(CGAffineTransform相关)

    仿射变换简介 仿射变换源于CoreGraphics框架,主要作用是绘制2D级别的图层,几乎所有iOS设备屏幕上的界面元素都是由CoreGraphics来负责绘制.而我们要了解的2D仿射变换是其下负责二 ...

随机推荐

  1. BZOJ3402: [Usaco2009 Open]Hide and Seek 捉迷藏

    3402: [Usaco2009 Open]Hide and Seek 捉迷藏 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 51  Solved: 4 ...

  2. 【转】Java 多线程(四) 多线程访问成员变量与局部变量

    原文网址:http://www.cnblogs.com/mengdd/archive/2013/02/16/2913659.html 先看一个程序例子: public class HelloThrea ...

  3. 笔记:java并发实践2

    public interface Executor { void execute(Runnable command); } 虽然Executor是一个简单的接口,但它为灵活且强大的异步任务框架提供了基 ...

  4. Shell入门第一课

    Shell是用C语言编写的程序. 几种常见的shell:bash.sh.csh.ksh等 bash是linux的默认标准shell, 完全兼容sh sh 是unix的默认 标准shell ash 是l ...

  5. Jetty监控线程使用情况的配置

    Jetty监控线程使用情况配置 第一步,配置xml文件 jetty-monitor.xml 参数说明: threads: 线程池中的线程 busyThreads: 使用中的线程 idleThreads ...

  6. firefox关于about:config的常用配置

    about:config是火狐的设置页面,火狐提供了不少高级设置选项在这里以便让你可以更加详细地控制火狐的运行方式.(官方不推荐用户手工修改about:config的设置.所以,如果你对于你想修改的内 ...

  7. hadoop权威指南 chapter2 MapReduce

    MapReduce MapReduce is a programming model for data processing. The model is simple, yet not too sim ...

  8. android媒体--stagefright概述【一】

    近期杂七杂八的忙碌着,前几天看了下这部分主要是stagefright模块的,所以更改下名字 做了挺长时间的android平台的媒体开发,对之前的分析进行一个阶段性的总结. 一.android结构图(上 ...

  9. [RxJS] Creation operator: of()

    RxJS is a lot about the so-called "operators". We will learn most of the important operato ...

  10. [Protractor] Getting Started With Protractor

    Protractor is an end-to-end testing library for AngularJS. Install: npm install -g protractor This w ...