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)的更多相关文章
- [Xcode 实际操作]二、视图与手势-(9)CGAffineTransform仿射变换的使用
目录:[Swift]Xcode实际操作 本文将演示使用视图对象的仿射变换功能,旋转视图对象. import UIKit class ViewController: UIViewController { ...
- CGAffineTransform
这个是CoreGraphics框架中的CGAffineTransform类,可用于设定UIView的transform属性.控制视图的缩放.旋转和平移操作.另称仿射变换矩阵. Quartz转换实现原理 ...
- CGAffineTransform相关函数
CoreGraphics.h CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI_2); [xxx setTransform ...
- IOSView翻转扭矩位移
CoreGraphics.h CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI_2);[xxx setTransform: ...
- iOS - 视图与手势(UIview & UIGestureRecognizer)
01 UIView视图的基本使用 --- 在根视图中添加2个UIView视图 //视图确实加载时调用 - (void)viewDidLoad { [super viewDidLoad]; // Do ...
- iOS开发--知识点总结
1 .全局变量,变量名前加下划线.和系统一致. 2 . nil指针为空 @“”字符串为空 (内容为空) == 判断内存地址 基本变量 对于一些基本类型 可以使用==来判断, ...
- iOS核心动画高级技巧之图层变换和专用图层(二)
iOS核心动画高级技巧之CALayer(一) iOS核心动画高级技巧之图层变换和专用图层(二)iOS核心动画高级技巧之核心动画(三)iOS核心动画高级技巧之性能(四)iOS核心动画高级技巧之动画总结( ...
- [Swift]Xcode实际操作
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- iOS下的2D仿射变换机制(CGAffineTransform相关)
仿射变换简介 仿射变换源于CoreGraphics框架,主要作用是绘制2D级别的图层,几乎所有iOS设备屏幕上的界面元素都是由CoreGraphics来负责绘制.而我们要了解的2D仿射变换是其下负责二 ...
随机推荐
- db file scattered read
SQL> set linesize 200 SQL> col name format a30 SQL> select SESSION_ID,NAME,P1,P2,P3,WAIT_TI ...
- buffer busy wait
什么是buffer busy wait? A session that reads or modifies a buffer in the SGA must first acquire the cac ...
- 【HDOJ】1011 Starship Troopers
第一道树形DP.很容易理解. #include <cstdio> #include <cstring> #include <cstdlib> #define MAX ...
- 开启Apache mod_rewrite模块(解决404 Not Found)
网站搭建完成了,进入登录界面就是访问不了. 原因大概是没有开启Apache mod_rewrite模块,或者没有配置完全. 步骤1: 启用mod_rewrite模块 在conf目录的httpd.con ...
- C++ deepin
访问类成员函数(cin.getline())方式是从访问结构成员变量方式衍生而来; C++结构体变量申明 struct关键字可省略; c++结构体变量声明初始化, = 可省略;但此需用在c++,大家都 ...
- 面试al tx
阿里: 一面: 1:写代码,给三个数组abc,每个数组若干数字,判断一个数字在不在三个数组中.用的map解决. 2:例举知道的排序,写出归并排序代码. 3:剩下的都是小题目了:包括三次握手,tc ...
- Cocos2D-X v3.0 alpha1环境搭建
周末看了下Cocos2D,感觉用起来还是挺爽的样子,跨平台,支持Windows, Linux, Mac, IOS, Android,WP...N多平台..还是C++开源滴,果断下下来研究下.. 最新版 ...
- 高性能 Socket 组件 HP-Socket v3.2.1-RC4 公布
HP-Socket 是一套通用的高性能 TCP/UDP Socket 组件,包括服务端组件.client组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C+ ...
- dojo 学习笔记之dojo.query - query(id) 与query(class)的差别
考虑这个样例:动态创建一个页面的时候,用new listtem()生成多个listitem, 且每一个listitem中都生成一个按钮button. 假设想要给每一个按钮都绑定一个click事件,用d ...
- lua pbc
先要将proto文件编译成.pb文件,然后再动态绑定实现lua protobuffer,这就需要了解云风做的pbc的项目,地址为:https://github.com/cloudwu/pbc/blob ...