iOS 2D绘图 (Quartz2D)之Transform(CTM,Translate,Rotate,scale)
前言:Quartz默认采用设备无关的user space来进行绘图,当context(画板)建立之后,默认的坐标系原点以及方向也就确认了,可以通过CTM(current transformation matrix)来修坐标系的原点。从数组图像处理的角度来说,就是对当前context state乘以一个状态矩阵。其中的矩阵运算开发者可以不了解
最初的状态和代码
#import "CustomView.h" @implementation CustomView - (void)drawRect:(CGRect)rect {
[super drawRect:rect];
//画一个矩形
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddRect(context, CGRectMake(,,, ));
CGContextSetFillColorWithColor(context,[UIColor blueColor].CGColor);
CGContextFillPath(context);
} -(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.opaque = NO;
self.layer.borderColor = [UIColor lightGrayColor].CGColor;
self.layer.borderWidth = 1.0;
}
return self;
} @end
在控制器里调用一下就可以了
CustomView * customView = [[CustomView alloc] initWithFrame:CGRectMake(, ,, )];
[self.view addSubview:customView];
图解
Translate
假如我们在绘制前,进行坐标系移动会是什么效果呢?
代码:
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
//画一个矩形
CGContextRef context = UIGraphicsGetCurrentContext();
//移动坐标系
CGContextTranslateCTM(context, , );
CGContextAddRect(context, CGRectMake(,,, ));
CGContextSetFillColorWithColor(context,[UIColor blueColor].CGColor);
CGContextFillPath(context);
}
效果:
代码中 我们还是在(10,10)点绘制,但是要注意,当前坐标系的原点已经移动到(10,10)了。
Rotate
在Transform的基础上 我们再Rotate 45度,注意CGContextRotateCTM传入的参数是弧度
代码
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
//画一个矩形
CGContextRef context = UIGraphicsGetCurrentContext();
//移动坐标系
CGContextTranslateCTM(context, , );
//旋转坐标系
CGContextRotateCTM(context, M_PI_4);
CGContextAddRect(context, CGRectMake(,,, ));
CGContextSetFillColorWithColor(context,[UIColor blueColor].CGColor);
CGContextFillPath(context);
}
效果
Scale
对于Scale相对来说,好理解一些,无非就是成比例放大或缩小
代码
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
//画一个矩形
CGContextRef context = UIGraphicsGetCurrentContext();
//移动坐标系
CGContextTranslateCTM(context, , );
//旋转坐标系
CGContextRotateCTM(context, M_PI_4);
//缩放坐标系
CGContextScaleCTM(context, 0.5, 0.5);
CGContextAddRect(context, CGRectMake(,,, ));
CGContextSetFillColorWithColor(context,[UIColor blueColor].CGColor);
CGContextFillPath(context);
}
效果
状态保存 恢复
在复杂的绘制图中,我们可能指向对一个subPath进行缩放移动旋转。这个时候,状态堆栈就起到作用了。
代码
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
//画一个矩形
CGContextRef context = UIGraphicsGetCurrentContext();
//保存状态 入栈
CGContextSaveGState(context);
//移动坐标系
CGContextTranslateCTM(context, , );
//旋转坐标系
CGContextRotateCTM(context, M_PI_4);
//缩放坐标系
CGContextScaleCTM(context, 0.5, 0.5);
CGContextAddRect(context, CGRectMake(,,, ));
CGContextSetFillColorWithColor(context,[UIColor blueColor].CGColor);
CGContextFillPath(context);
//推出栈顶部状态
CGContextRestoreGState(context);
//这里坐标系已经回到了最开始的状态
CGContextAddRect(context, CGRectMake(, , , ));
CGContextFillPath(context);
}
效果
Affine Transforms
可以通过以下方法先创建放射矩阵,然后在把放射矩阵映射到CTM
CGAffineTransform
CGAffineTransformTranslate
CGAffineTransformMakeRotation
CGAffineTransformRotate
CGAffineTransformMakeScale
CGAffineTransformScale
iOS 2D绘图 (Quartz2D)之Transform(CTM,Translate,Rotate,scale)的更多相关文章
- iOS 2D绘图详解(Quartz 2D)之Transform(CTM,Translate,Rotate,Scale)
前言:Quartz默认采用设备无关的user space来进行绘图,当context(画板)建立之后,默认的坐标系原点以及方向也就确认了,可以通过CTM(current transformation ...
- iOS 2D绘图 (Quartz2D)之阴影和渐变(shadow,Gradient)
原博地址:http://blog.csdn.net/hello_hwc/article/details/49507881 Shadow Shadow(阴影) 的目的是为了使UI更有立体感,如图 sha ...
- iOS 2D绘图 (Quartz2D)之路径(stroke,fill,clip,subpath,blend)
像往常一样 这个系列的博客是跟着大神的脚步来的.按照往例 在此贴出原博客的出处: http://blog.csdn.net/hello_hwc?viewmode=list我对大神的崇拜之情 如滔滔江水 ...
- iOS 2D绘图 (Quartz2D)之路径(点,直线,虚线,曲线,圆弧,椭圆,矩形)
博客原地址:http://blog.csdn.net/hello_hwc?viewmode=list 让我们继续跟着大神的脚步前进吧.这一次 我们学习一些Quartz 2D 最基本的一些用法. 前言: ...
- iOS 2D绘图详解(Quartz 2D)之路径(点,直线,虚线,曲线,圆弧,椭圆,矩形)
前言:一个路径可以包含由一个或者多个shape以及子路径subpath,quartz提供了很多方便的shape可以直接调用.例如:point,line,Arc(圆弧),Curves(曲线),Ellip ...
- iOS 2D绘图 (Quartz 2D) 概述
本篇博客原文地址:http://blog.csdn.net/hello_hwc?viewmode=list 由于自己的项目需要,从网络上下载了许多关于绘制图形的demo,只是用在自己的项目中,很多地方 ...
- iOS 2D绘图详解(Quartz 2D)之概述
前言:最近在研究自定义控件,由于想要彻底的定制控件的视图还是要继承UIView,虽然对CALayer及其子类很熟练,但是对Quartz 2D这个强大的框架仍然概念模棱两可.于是,决定学习下,暂定7篇文 ...
- iOS 2D绘图详解(Quartz 2D)之Bitmap
什么是Bitmap? Bitmap叫做位图,每一个像素点由1-32bit组成.每个像素点包括多个颜色组件和一个Alpha组件(例如:RGBA). iOS中指出如下格式的图片 JPEG, GIF, PN ...
- iOS 2D绘图详解(Quartz 2D)之阴影和渐变(Shadow,Gradient)
前言:这个系列写道这里已经是第五篇了,本文会介绍下阴影和渐变的基础知识,以及一些基本的Demo Code展示,应该还会有两篇,介绍下Bitmap绘制以及Pattern等知识. Shadow shado ...
随机推荐
- Thinking in Java——笔记(20)
Annotations They provide information that you need to fully describe your program, but that cannot b ...
- sql执行循序
(8) select (9) distinct (11) top 1 (6) Table1.id,COUNT(Table1.name) as nameCount (1) from Table1 (3) ...
- Verilog学习笔记基本语法篇(十一)········ 常用系统函数
1)系统任务:$monitor 格式: $monitor(p1,p2,p3...pn); $monitor; $monitoron; $monitoroff; 任务$monitor提供了监控输出列 ...
- 【系统架构】IT职业技能图谱(点开大图查看)
本文地址 1 程序开发语言综述 2 iOS开发工程师必备技能 3 运维工程师必备技能 4 前端工程师必备技能 5 大数据工程师必备技能 6 云计算工程师必备技能 7 安全工程师必备技能 8 移动无线测 ...
- CSS3与页面布局学习笔记(七)——前端预处理技术(Less、Sass、CoffeeScript、TypeScript)
CSS不像其它高级语言一样支持算术运算.变量.流程控制与面向对象特性,所以CSS样式较多时会引起一些问题,如修改复杂,冗余,某些别的语言很简单的功能实现不了等.而javascript则是一种半面向对象 ...
- 本地部署arcgis by eclipse
首次来博客园发帖,从本地部署arcgis api开始吧: 首先还是下载arcgis的api包开始,在中国区官网下载arcgis包: 1.http://support.esrichina.com.cn/ ...
- Bootstrap之字体图标
优点:1.减少请求 2.容易控制样式 所在位置:在下载的bootstrap文件中的fonts文件夹存放字体图标 默认路径为当前目录下,如需修改路径,则需在bootstrap.css中查找font-fa ...
- [python]初试页面抓取——抓取沪深股市交易龙虎榜数据
[python]抓取沪深股市交易龙虎榜数据 python 3.5.0下运行 没做自动建立files文件夹,需要手动在py文件目录下建立files文件夹后运行 #coding=utf-8 import ...
- javascript随笔20160808
var jsondata=$.parseJSON(@ViewBag.x); //转换为Json数据 var fruits = ["Banana", "Orange&quo ...
- C# 生成字符串的 CheckSum
C# 生成字符串的 CheckSum private static string CheckSum(string message) { char[] chars = message.ToCharArr ...