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

  1. iOS 2D绘图详解(Quartz 2D)之Transform(CTM,Translate,Rotate,Scale)

    前言:Quartz默认采用设备无关的user space来进行绘图,当context(画板)建立之后,默认的坐标系原点以及方向也就确认了,可以通过CTM(current transformation ...

  2. iOS 2D绘图 (Quartz2D)之阴影和渐变(shadow,Gradient)

    原博地址:http://blog.csdn.net/hello_hwc/article/details/49507881 Shadow Shadow(阴影) 的目的是为了使UI更有立体感,如图 sha ...

  3. iOS 2D绘图 (Quartz2D)之路径(stroke,fill,clip,subpath,blend)

    像往常一样 这个系列的博客是跟着大神的脚步来的.按照往例 在此贴出原博客的出处: http://blog.csdn.net/hello_hwc?viewmode=list我对大神的崇拜之情 如滔滔江水 ...

  4. iOS 2D绘图 (Quartz2D)之路径(点,直线,虚线,曲线,圆弧,椭圆,矩形)

    博客原地址:http://blog.csdn.net/hello_hwc?viewmode=list 让我们继续跟着大神的脚步前进吧.这一次 我们学习一些Quartz 2D 最基本的一些用法. 前言: ...

  5. iOS 2D绘图详解(Quartz 2D)之路径(点,直线,虚线,曲线,圆弧,椭圆,矩形)

    前言:一个路径可以包含由一个或者多个shape以及子路径subpath,quartz提供了很多方便的shape可以直接调用.例如:point,line,Arc(圆弧),Curves(曲线),Ellip ...

  6. iOS 2D绘图 (Quartz 2D) 概述

    本篇博客原文地址:http://blog.csdn.net/hello_hwc?viewmode=list 由于自己的项目需要,从网络上下载了许多关于绘制图形的demo,只是用在自己的项目中,很多地方 ...

  7. iOS 2D绘图详解(Quartz 2D)之概述

    前言:最近在研究自定义控件,由于想要彻底的定制控件的视图还是要继承UIView,虽然对CALayer及其子类很熟练,但是对Quartz 2D这个强大的框架仍然概念模棱两可.于是,决定学习下,暂定7篇文 ...

  8. iOS 2D绘图详解(Quartz 2D)之Bitmap

    什么是Bitmap? Bitmap叫做位图,每一个像素点由1-32bit组成.每个像素点包括多个颜色组件和一个Alpha组件(例如:RGBA). iOS中指出如下格式的图片 JPEG, GIF, PN ...

  9. iOS 2D绘图详解(Quartz 2D)之阴影和渐变(Shadow,Gradient)

    前言:这个系列写道这里已经是第五篇了,本文会介绍下阴影和渐变的基础知识,以及一些基本的Demo Code展示,应该还会有两篇,介绍下Bitmap绘制以及Pattern等知识. Shadow shado ...

随机推荐

  1. JAVA基础,字符串

    字符串String(一个字符数组,常量,不可变): 1. 创建并初始化字符串: 1). 使用字符串常量直接初始化 String s="hello!"; 2). 使用构造方法创建并初 ...

  2. C标准头文件<stdlib.h>

    是个大杂烩,里面声明了从动态内存分配到常用算法等各种函数和宏 #数据类型 **size_t** **wchar_t** **div_t**是一个结构体类型,也是div()返回的类型 **ldiv_t* ...

  3. 自己写一个 jQuery 插件

    我知道这一天终将会到来,现在,它来了. 需求 开发 SharePoint 的 CSOM 应用时,经常需要在网页上输出一些信息. 这种需求和 alert 的弹窗.F12 的断点查看信息的场景是不一样的: ...

  4. 关于举办 2015年 Autodesk 助力云应用项目开发活动通知

    各位尊敬的Autodesk 合作伙伴,大家好! 相信您在过去的一年里应该对Autodesk最新的云服务技术有所了解,您是不是曾经闪现过一些很好的想法,却由于不确定是否真实可行,或担心没有技术支持来帮助 ...

  5. iOS--UILable自适应大小

    #import "ViewController.h" @interface ViewController () @property(strong,nonatomic) UILabe ...

  6. iOS时间个性化设置设置

    现在在很多项目中,不会直接显示时间,很多时候都是显示“刚刚”,”XX分钟前”,等等字样,那么他们是怎么实现的呢 ? .新建一个NSDate的类目:NSDate+XMGExtension NSDate+ ...

  7. 深入浅出React Native 3: 从零开始写一个Hello World

    这是深入浅出React Native的第三篇文章. 1. 环境配置 2. 我的第一个应用 将index.ios.js中的代码全部删掉,为什么要删掉呢?因为我们准备从零开始写一个应用~学习技术最好的方式 ...

  8. IOS基础之UILineBreakModeWordWrap

    UILineBreakModeWordWrap详细解释如下:  typedef enum {     UILineBreakModeWordWrap = 0,     UILineBreakModeC ...

  9. js操作数组

    一.数组的声明方式: var colors = new Array();//创建数组 var colors = new Array(20);//创建20个长度的数组 var colors = new ...

  10. Hadoop学习2

    搭建伪分布式完成之后: 伪分布式安装详细介绍:http://www.powerxing.com/install-hadoop/ 练习1 编写Java程序实现以下函数: 1.向HDFS中上传文件 2.从 ...