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(10,10,40, 20));
    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(100, 100,100, 100)];
[self.view addSubview:customView];

图解



Translate

假如我们在绘制前,进行坐标系移动会是什么效果呢?

代码:

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    //画一个矩形
    CGContextRef context = UIGraphicsGetCurrentContext();
    //移动坐标系
    CGContextTranslateCTM(context, 10, 10);
    CGContextAddRect(context, CGRectMake(10,10,40, 20));
    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, 10, 10);
    //旋转坐标系
    CGContextRotateCTM(context, M_PI_4);
    CGContextAddRect(context, CGRectMake(10,10,40, 20));
    CGContextSetFillColorWithColor(context,[UIColor blueColor].CGColor);
    CGContextFillPath(context);
}

效果



Scale

对于Scale相对来说,好理解一些,无非就是成比例放大或缩小

代码

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    //画一个矩形
    CGContextRef context = UIGraphicsGetCurrentContext();
    //移动坐标系
    CGContextTranslateCTM(context, 10, 10);
    //旋转坐标系
    CGContextRotateCTM(context, M_PI_4);
    //缩放坐标系
    CGContextScaleCTM(context, 0.5, 0.5);
    CGContextAddRect(context, CGRectMake(10,10,40, 20));
    CGContextSetFillColorWithColor(context,[UIColor blueColor].CGColor);
    CGContextFillPath(context);
}

效果



状态保存 恢复

在复杂的绘制图中,我们可能指向对一个subPath进行缩放移动旋转。这个时候,状态堆栈就起到作用了。

代码

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    //画一个矩形
    CGContextRef context = UIGraphicsGetCurrentContext();
    //保存状态 入栈
    CGContextSaveGState(context);
    //移动坐标系
    CGContextTranslateCTM(context, 10, 10);
    //旋转坐标系
    CGContextRotateCTM(context, M_PI_4);
    //缩放坐标系
    CGContextScaleCTM(context, 0.5, 0.5);
    CGContextAddRect(context, CGRectMake(20,20,40, 20));
    CGContextSetFillColorWithColor(context,[UIColor blueColor].CGColor);
    CGContextFillPath(context);
    //推出栈顶部状态
    CGContextRestoreGState(context);
    //这里坐标系已经回到了最开始的状态
    CGContextAddRect(context, CGRectMake(0, 0, 20, 20));
    CGContextFillPath(context);
}

效果



Affine Transforms

可以通过以下方法先创建放射矩阵,然后在把放射矩阵映射到CTM

CGAffineTransform
CGAffineTransformTranslate
CGAffineTransformMakeRotation
CGAffineTransformRotate
CGAffineTransformMakeScale
CGAffineTransformScale

时间: 2024-08-21 00:14:33

iOS 2D绘图 (Quartz2D)之Transform(CTM,Translate,Rotate,scale)的相关文章

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

像往常一样 这个系列的博客是跟着大神的脚步来的.按照往例 在此贴出原博客的出处: http://blog.csdn.net/hello_hwc?viewmode=list 我对大神的崇拜之情 如滔滔江水 巴拉巴拉的 ......... 言归正传 Stroke-描边 影响描边的因素 线的宽度-CGContextSetLineWidth 交叉线的处理方式-CGContextSetLineJoin 线顶端的处理方式-CGContextSetLineCap 进一步限制交叉线的处理方式-CGContext

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

原博地址:http://blog.csdn.net/hello_hwc/article/details/49507881 Shadow Shadow(阴影) 的目的是为了使UI更有立体感,如图 shadow 主要有三个影响因素 x off-set 决定阴影沿着X的偏移量 y off-set 决定阴影沿着y的偏移量 blur value 决定了阴影的边缘区域是不是模糊的 其中不同的blur效果的图 注意: shadow也是和绘制状态相关的,意味着如果仅仅绘制一个subPath的shadow,注意s

0064 2D转换综合写法以及顺序问题:translate rotate scale

2D 转换综合写法以及顺序问题 知识要点 同时使用多个转换,其格式为 transform: translate() rotate() scale() 顺序会影响到转换的效果(先旋转会改变坐标轴方向) 但我们同时有位置或者其他属性的时候,要将位移放到最前面 代码演示 div:hover { transform: translate(200px, 0) rotate(360deg) scale(1.2) } <!DOCTYPE html> <html lang="en"&

iOS 2D绘图详解(Quartz 2D)之路径(stroke,fill,clip,subpath,blend)

Stroke-描边 影响描边的因素 线的宽度-CGContextSetLineWidth 交叉线的处理方式-CGContextSetLineJoin 线顶端的处理方式-CGContextSetLineCap 进一步限制交叉线的处理方式 -CGContextSetMiterLimit 是否要虚线-Line dash pattern 颜色控件-CGContextSetStrokeColorSpace 画笔颜色-CGContextSetStrokeColor/CGContextSetStrokeCol

CSS3 2D 转换【旋转transform:rotate(30deg); 移动transform: translate(50px,100px); 放大缩小transform:scale(2,4)】

CSS3 2D 转换 CSS3 转换 CSS3转换,我们可以移动,比例化,反过来,旋转,和拉伸元素. 它是如何工作? 变换的效果,让某个元素改变形状,大小和位置. 您可以转换您使用2D或3D元素. 浏览器支持 表格中的数字表示支持该属性的第一个浏览器版本号. 紧跟在 -webkit-, -ms- 或 -moz- 前的数字为支持该前缀属性的第一个浏览器版本号. Property           transform 36.0 4.0 -webkit- 10.0 9.0 -ms- 16.0 3.5

WPF学习05:2D绘图之Transform

通过上一篇WPF学习WPF学习04:2D绘图之Shape我们可以进行基本图形的绘制. 涉及到图形应用编程,仅仅绘制基本的图形当然是不够的,我们可以借助WPF提供的Transform类,对控件进行变形操作. 例子 由一个三角形旋转而成的组合图形: 后台代码: private void Window_Loaded(object sender, RoutedEventArgs e) { for (int i = 0; i < 36; i++) { var polygon = new Polygon()

ios (Quartz 2D绘图)各种绘图方式及相机的使用

一: 具体使用的细节,本人也是参考http://blog.163.com/wkyuyang_001/blog/static/10802122820133190545227/ 下面介绍具体使用Quartz 2D绘图实现画图板功能 .m文件中,dog的实现如连接中所示一样的 <pre name="code" class="objc">#import "drawTestView.h" #import "Dog.h" @i

Quartz 2D 绘图,图像变换

IOS的quartz 2d 绘图,绘图有时候需要进行图形变换. 切记:Quartz 2D的变换都是以CG 开头的,不要和Core Animation的搞混了,Core animatin的是CA开头的. IOS提供了一组可以进行图像变换的API接口,一般的图像变换分为,平移.缩放.旋转. 平移 void CGContextTranslateCTM(CGContextRef c, CGFloat tx, CGFloat ty); 缩放 void CGContextScaleCTM(CGContext

Quartz-2D绘图之概览

最近公司新项目需求要把数据图形化,趁着这个机会,重温了下Quarts-2D这个强大的跨平台2D绘图引擎. 一.Quartz概述 1.Quartz 2D是一个二维的绘图引擎,支持iOS和Mac OS平台. 2.功能:可以用来进行基本路径的绘制.透明度.描影.绘制阴影.透明层.颜色管理.反锯齿.PDF文档生成和PDF元数据访问等 二.Quartz操作 1.绘图顺序:Quartz既然是一个绘图引擎,那么画画的先后顺序是非常重要的,下图便展示了它的操作顺序,相信大家都很熟悉这幅图 2.绘制目标:Grap