在iOS中绘图一般分为以下几个步骤:

1.获取绘图上下文

2.创建并设置路径

3.将路径添加到上下文

4.设置上下文状态

5.绘制路径

6.释放路径

在UIKit中默认已经为我们准备好了一个图形上下文对象,在UI控件的drawRect:方法(这个方法在 loadView、viewDidLoad方法后执行)中我们可以通过UIKit封装函数UIGraphicsGetCurrentContext()方 法获得这个图形上下文(注意在其他UI控件方法中无法取得这个对象),然后我们只要按照绘图步骤一步步执行即可。下面自定义一个KCView继承自 UIView,重写drawRect:方法绘制两条直线说明上面绘图的步骤:

//
// DLView.m
// Demo0223
//
// Created by wiseman on 16/2/23.
// Copyright (c) 2016年 wiseman. All rights reserved.
// #import "DLView.h" @implementation DLView /*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/ //绘图只能在此方法中调用,否则无法得到当前图形上下文
-(void)drawRect:(CGRect)rect{
//1.取得图形的上下文对象
CGContextRef context = UIGraphicsGetCurrentContext(); /*
//2.绘制路径
CGContextMoveToPoint(context, 20, 50);
CGContextAddLineToPoint(context, 20, 100);
CGContextAddLineToPoint(context, 300, 100);
//封闭路径
CGContextClosePath(context); //3.设置图形上下文属性
[[UIColor redColor] setStroke];//设置红色边框
[[UIColor blueColor] setFill];//设置蓝色填充
//[[UIColor blueColor]set];//同时设置填充和边框色 //4.绘制路径
CGContextDrawPath(context, kCGPathFillStroke);
*/ /*
绘制矩形
*/
// [self drawRectWithContext:context]; /*
绘制椭圆
*/
// [self drawEllipseWithContext:context]; /*
绘制弧形
*/
// [self drawArc:context]; /*
绘制贝塞尔曲线
*/
// [self drawCurve:context]; /*
文字绘制
*/
[self drawText:context]; } #pragma mark - 绘制文字
-(void)drawText:(CGContextRef)context{
//绘制到指定的区域内容
NSString *str=@"Star Walk is the most beautiful stargazing app you’ve ever seen on a mobile device. It will become your go-to interactive astro guide to the night sky, following your every movement in real-time and allowing you to explore over 200, 000 celestial bodies with extensive information about stars and constellations that you find.";
CGRect rect= CGRectMake(, , , );
UIFont *font=[UIFont systemFontOfSize:];//设置字体
UIColor *color=[UIColor redColor];//字体颜色
NSMutableParagraphStyle *style=[[NSMutableParagraphStyle alloc]init];//段落样式
NSTextAlignment align=NSTextAlignmentLeft;//对齐方式
style.alignment=align;
[str drawInRect:rect withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color,NSParagraphStyleAttributeName:style}];
} #pragma mark - 绘制贝塞尔曲线
-(void)drawCurve:(CGContextRef)context{
//绘制曲线
CGContextMoveToPoint(context, , );//移动到起始位置
/*绘制二次贝塞尔曲线
c:图形上下文
cpx:控制点x坐标
cpy:控制点y坐标
x:结束点x坐标
y:结束点y坐标 CGContextAddQuadCurveToPoint(CGContextRef c, CGFloat cpx, CGFloat cpy, CGFloat x, CGFloat y)
*/
CGContextAddQuadCurveToPoint(context, , , , ); [[UIColor clearColor] setFill];
[[UIColor blackColor] setStroke]; /*绘制三次贝塞尔曲线
c:图形上下文
cp1x:第一个控制点x坐标
cp1y:第一个控制点y坐标
cp2x:第二个控制点x坐标
cp2y:第二个控制点y坐标
x:结束点x坐标
y:结束点y坐标
*/
CGContextMoveToPoint(context, , );
CGContextAddCurveToPoint(context, , , , , , ); [[UIColor redColor] setStroke];
[[UIColor clearColor] setFill]; CGContextDrawPath(context, kCGPathFillStroke);
} #pragma mark - 绘制弧形
-(void)drawArc:(CGContextRef)context{
//添加弧形对象
/*
x:中心点x坐标
y:中心点y坐标
radius:半径
startAngle:起始弧度
endAngle:终止弧度
closewise:是否逆时针绘制,0则顺时针绘制 CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)
*/
CGContextAddArc(context, , , , , M_PI_2, );
[[UIColor blueColor]setStroke];
[[UIColor clearColor]setFill];
CGContextDrawPath(context, kCGPathFillStroke); } #pragma mark - 绘制椭圆
-(void)drawEllipseWithContext:(CGContextRef)context{
//添加对象,绘制椭圆(圆形)的过程首先也是创建一个矩形
CGRect rect = CGRectMake(, , , );
CGContextAddEllipseInRect(context, rect);
//设置属性
[[UIColor redColor] set];
CGContextDrawPath(context, kCGPathFillStroke);
} #pragma mark 绘制矩形
-(void)drawRectWithContext:(CGContextRef)context{
//添加矩形对象
CGRect rect=CGRectMake(, , 280.0, 50.0);
CGContextAddRect(context,rect);
//设置属性
[[UIColor blueColor]set];
//绘制
CGContextDrawPath(context, kCGPathFillStroke);
} #pragma mark 绘制矩形(利用UIKit的封装方法)
-(void)drawRectByUIKitWithContext:(CGContextRef)context{
CGRect rect= CGRectMake(, , 280.0, 50.0);
CGRect rect2=CGRectMake(, , 280.0, 50.0);
//设置属性
[[UIColor yellowColor]set];
//绘制矩形,相当于创建对象、添加对象到上下文、绘制三个步骤
UIRectFill(rect);//绘制矩形(只有填充) [[UIColor redColor]setStroke];
UIRectFrame(rect2);//绘制矩形(只有边框)
} @end

Quartz 2D中的基本图形绘制的更多相关文章

  1. Quartz 2D中CGContextSaveGState与UIGraphicsPushContext

    CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Context是图形上下文 ...

  2. Quartz 2D编程指南(2)图形上下文(Graphics Contexts)

    Graphics Contexts       一个Graphics Context表示一个绘制目标(也能够理解为图形上下文).它包括绘制系统用于完毕绘制指令的绘制參数和设备相关信息.Graphics ...

  3. C#中的GDI+图形绘制方法

    GDI+图形绘制方法 1.首先对于绘制图形,必须的先将命名空间导入:using System.Drawing.Drawing2D; 2.然后在一个事件中写入程序 首先先将Graphics这个对象实例化 ...

  4. Quartz 2D绘制简单图形

    在Quartz 2D中,绘图是通过图形上下文进行绘制的,以下绘制几个简单的图形 首先先创建一个QuartzView.swift文件继承自UIView,然后实现drawRect方法: import UI ...

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

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

  6. Quartz 2d绘图

    今天看了一下Quartz 2D绘图,我只想说:不要把绘图和动画那些东西当做一个很复杂的东西,其实只要你认真看还是可以理解的.他们并不难.啰嗦了几句,现在直接进入正题: 前提是我们必须新建一个singl ...

  7. iPhone之Quartz 2D系列--编程指南(1)概览

    以下几遍关于Quartz 2D博文都是转载自:http://www.cocoachina.com/bbs/u.php?action=topic&uid=38018 iPhone之Quartz ...

  8. Quartz 2D 概述

    Quartz 2D是一个二维图形绘制引擎,支持iOS环境和Mac OS X环境.我们可以使用Quartz 2D API来实现许多功能,如基本路径的绘制.透明度.描影.绘制阴影.透明层.颜色管理.反锯齿 ...

  9. iOS - Quartz 2D 二维绘图

    1.Quartz 2D 简介 Quartz 2D 属于 Core Graphics(所以大多数相关方法的都是以 CG 开头),是 iOS/Mac OSX 提供的在内核之上的强大的 2D 绘图引擎,并且 ...

随机推荐

  1. 图片上传插件用法,net语法【二】

    之前一直写过KindeEditor中的小控件作为单独上次,但业务要求需要另一种方式 现在改用ajaxfileupload.js试试,这个一百度 一.首页引用 <script src=" ...

  2. JSON文件处理

    牛X的JSON解析JSON字符串显示字典键值 public void ResolveJson() { //定义的JSON字符串,注意JSON的格式 string str = @” { “”Name”” ...

  3. 动态多条件查询分页以及排序(一)--MVC与Entity Framework版url分页版

    一.前言 多条件查询分页以及排序  每个系统里都会有这个的代码 做好这块 可以大大提高开发效率  所以博主分享下自己的6个版本的 多条件查询分页以及排序 二.目前状况 不论是ado.net 还是EF ...

  4. background-size做自适应的背景图

    background-size做自适应的背景图 在我们做页面布局的时候往往会遇到这样的情况当我固定一个元素的尺寸,在像元素加入背景的时候发现背景图片的原始尺寸很大,当我把背景图写入时往往超过元素很大一 ...

  5. VS发布网站步骤(先在vs上发布网站到新的文件夹,然后挂到iis上面)

    VS发布网站步骤(先在vs上发布网站到新的文件夹,然后挂到iis上面) 首先用vs2010打开一个Asp.Net项目,   也可以通过vs菜单->生成->发布网站   选择发布网站的路径 ...

  6. CSS3 Filter滤镜效果

    关注到它是在一次分享会当中,很神奇,只需写一行代码就可以变身很美的视觉效果,这就是CSS3滤镜. 语法 filter:fuction(param); 如今浏览器支持情况相比以前乐观很多,点击查看兼容 ...

  7. Mssql 行转列

    ) set @sql='' --初始化变量@sql --变量多值赋值 ,,'')--去掉首个',' set @sql=' select * from( select objectid,name,jyj ...

  8. scala优点以及eclipse上安装scala插件

    可拓展 (面向对象,函数式编程) 静态类型化 (可检验,安全重构) 兼容JAVA (类库调用,互操作) 支持并发控制 (强计算能力,自定义其他控制结构) 语法简洁 (代码行短,类型推断,抽象控制) 插 ...

  9. Struts2拦截器配置

    1. 理解拦截器 1.1. 什么是拦截器: 拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作.拦截是AO ...

  10. struts2.3 创建工程

    1:在该网站下载struts2.3.16.3,目前为最新版.http://www.struts.apache.org/download.cgi 不妨下载“Full Distribution”版本 下载 ...