CoreGraphics--画线/圆/矩形
- (void)drawRect:(CGRect)rect {
// Drawing code
NSLog(@"drawRect自动调用");
//画图步骤
//获取上下文(/画笔/绘图环境)
CGContextRef context = UIGraphicsGetCurrentContext();
//设置画笔颜色
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
//线条的宽度
CGContextSetLineWidth(context, 4);
//开始画
//1.画一条直线
// [self drawOneLineWith:context];
//2-1.画多条线
// [self drawManyLines:context];
//2-2
// [self drawManyLines2:context];
//3画虚线
// [self drawDashLine:context];
//4 画矩形
// [self drawRectShape:context];
//5.填充的矩形
// [self drawFillRect:context];
//6 画椭圆
// [self drawCircle:context];
//7.填充的椭圆
// [self drawFillCircle:context];
//8 显示文字
// [self drawText];
//9.显示图片
[self drawImage];
// 10绘制曲线
//11圆弧
}
#pragmark mark 11 圆弧
- (void)drawArc{
//画布
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextBeginPath(ctx);
//圆弧
* @param c 当前图形
* @param x 圆弧的中心点坐标x
* @param y 曲线控制点的y坐标
* @param radius 指定点的x坐标值
* @param startAngle 弧的起点与正X轴的夹角,
* @param endAngle 弧的终点与正X轴的夹角
* @param clockwise 指定1创建一个顺时针的圆弧,或是指定0创建一个逆时针圆弧
*
*/
CGContextAddArc(ctx, 100, 100, 50, 0,M_PI/2, 0);
CGContextSetLineWidth(ctx, 3); //线宽
// CGContextStrokePath(ctx);//描线
CGContextFillPath(ctx); //填充
}
#pragma mark 10 二次曲线
- (void)drawCurve {
// 获取当前图形,视图推入堆栈的图形,相当于你所要绘制图形的图纸
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 创建一个新的空图形路径。
CGContextBeginPath(ctx);
//开始点
CGContextMoveToPoint(ctx, 160, 100);
/** * @brief 在指定点追加二次贝塞尔曲线,通过控制点和结束点指定曲线。
@param c 当前图形 * @param cpx 曲线控制点的x坐标 *
@param cpy 曲线控制点的y坐标 *
@param x 指定点的x坐标值 *
@param y 指定点的y坐标值 * */
CGContextAddQuadCurveToPoint(ctx, 160, 50, 190, 50);
CGContextSetLineWidth(ctx, 20);
CGContextSetStrokeColorWithColor(ctx, [UIColor brownColor].CGColor);
CGContextStrokePath(ctx);
}
#pragma makr -- 9 显示图片
- (void)drawImage{
[[UIImage imageNamed:@"收藏-128"] drawInRect:CGRectMake(0, 0, 100, 100)];
}
#pragma mark -- 8 显示文字
- (void)drawText{
//起点(attribute 中设这参数)
[@"aaabbbcccddd" drawAtPoint:CGPointMake(100, 100) withAttributes:NULL];
//
[@"asdfasdfaf" drawInRect:CGRectMake(100, 150, 200, 200) withAttributes:NULL];
}
#pragma mark -- 7 画填充的 椭圆
- (void)drawFillCircle:(CGContextRef)context{
//填充的颜色
[[UIColor redColor] setFill];
CGContextFillEllipseInRect(context, CGRectMake(40, 40, 200, 300));
}
#pragma mark -- 6 画椭圆
- (void)drawCircle:(CGContextRef)context{
CGContextStrokeEllipseInRect(context, CGRectMake(40, 40, 300, 200));
}
#pragma mark -- 5填充的矩形
- (void)drawFillRect:(CGContextRef)context{
//设置填充颜色
[[UIColor yellowColor] setFill];
CGContextFillRect(context, CGRectMake(40, 40, 250, 300));
}
#pragma mark --4画矩形
- (void)drawRectShape:(CGContextRef)context{
CGContextStrokeRect(context, CGRectMake(40, 40, 200, 300));
//同理(stroke 画图方法; 也有add..方法 )
// CGContextAddRect(context, CGRectMake(40, 40, 200, 300));
// CGContextStrokePath(context);
}
#pragma mark -- 3 画虚线
- (void)drawDashLine:(CGContextRef)context{
//设置虚线类型
// CGFloat lengths[] = {5,15,5}; //长5 空隙15 长5 ...
CGFloat lengths[] = {5,5};
CGContextSetLineDash(context, 0, lengths, sizeof(lengths)/sizeof(lengths[0]));
// CGContextSetLineDash(<#CGContextRef c#>, <#CGFloat phase#>, <#const CGFloat *lengths#>, <#size_t count#>)
//phase 开始点 跳过多少个点数(0代表从头画)
//lengths 虚线的样式
//count 长度
//起点
CGContextMoveToPoint(context, 50, 50);
//画线
CGContextAddLineToPoint(context, 200, 200);
CGContextStrokePath(context);
//第二段设置成 直线 NULL
CGContextSetLineDash(context, 0, NULL, 0);
//需要重新设置起点
CGContextMoveToPoint(context, 200, 200);
CGContextAddLineToPoint(context, 250,400);
CGContextStrokePath(context);
}
#pragma mark --画多条线 (方式二)
- (void)drawManyLines2:(CGContextRef)context{
CGPoint point[] = {CGPointMake(50, 50),CGPointMake(100, 100),CGPointMake(100, 200)};
//计算数组中元素个数
// int count = sizeof(数组名)/size(sizeof(a[0]));
//个数
CGContextAddLines(context, point, sizeof(point)/sizeof(point[0]));
//闭合
CGContextClosePath(context);
//开始画
CGContextStrokePath(context);
}
#pragma mark --画多条线 (方式一)
- (void)drawManyLines:(CGContextRef)context{
//例如:三角形
CGContextMoveToPoint(context, 200, 100);
CGContextAddLineToPoint(context, 100, 200);
CGContextAddLineToPoint(context, 300, 200);
//回到起点
// CGContextAddLineToPoint(context, 200, 100);
//按方法2 : 闭合(起点和终点连线)
CGContextClosePath(context);
CGContextStrokePath(context);
}
#pragma mark -- 1 画一条线
- (void)drawOneLineWith:(CGContextRef)context{
//起点
CGContextMoveToPoint(context, 50, 50);
//终点:画线
CGContextAddLineToPoint(context, 200, 200);
//开始画
CGContextStrokePath(context); //画线
//CGContextFillPath(context); //填充路径
}
CoreGraphics--画线/圆/矩形的更多相关文章
- 使用gimp画线、矩形、圆等
使用gimp画线.矩形.圆等 https://blog.csdn.net/tody_guo/article/details/7628508 2012年06月03日 19:08:47 Tody Guo ...
- PHP合成图片、生成文字、居中对齐、画线、矩形、三角形、多边形、图片抗锯齿、不失真 高性能源码示例
function generateImg($source, $text1, $text2, $text3, $font = './msyhbd.ttf') { $date = '' . date ( ...
- H5中画图标签Canvas---画矩形,画线,画圆,渐变色,图形载入
一: 1.鼠标监视坐标值 <!DOCTYPE html> <head> <meta charset=UTF-8> <title>canvas的演示< ...
- IOS Quartz 各种绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等
// Only override drawRect: if you perform custom drawing. // An empty implementation adversely affec ...
- OpenCV各种绘制调用:线,矩形,圆,椭圆,文字
OpenCV提供了各种绘制接口,可以往图片里画各种东西,这种功能可以为以后在图像上标记一些信息方便调试 // drawcall.cpp: 定义控制台应用程序的入口点. // #include &quo ...
- 【液晶模块系列基础视频】4.1.X-GUI图形界面库-画线画圆等函数简介
[液晶模块系列基础视频]4.1.X-GUI图形界面库-画线画圆等函数简介 ============================== 技术论坛:http://www.eeschool.org 博客地 ...
- 纯JS画点、画线、画圆的方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- IOS 绘制基本图形( 画圆、画线、画圆弧、绘制三角形、绘制四边形)
// 当自定义view第一次显示出来的时候就会调用drawRect方法- (void)drawRect:(CGRect)rect { // 1.获取上下文 CGContextRef ctx = UIG ...
- LCD编程_画点线圆
上篇博客中进行了lcd的简单测试,这篇博客将进行更加复杂的测试——画点.画线.画圆.画线和画圆是在画点的基础上实现的,因此本篇博客重点实现画点操作. 先抛出这样的一个问题,已知: (x,y)的坐标: ...
随机推荐
- 如何使excel表格的内容自动添加前缀
一.假设是要在一列的单元格内容前加上固定的内容,则 方法一在原单元格实现,分两种情况 如果原单元格的内容是数字内容,要在原数字前添加"ABC"这样的前缀则选中这些单元格----右键 ...
- 通过浏览器navigator判断浏览器版本或者手机类型&&判断微信访问
javascript 的navigator属性,不常用,但是用处也不少,主要用处是在做浏览器兼容的问题的时候,现在有的网站已经不兼容IE6,用户假如用IE6浏览网页的话,会提示浏览器升级等信息.或者判 ...
- HDU5057(分块)
Argestes and Sequence Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- Orcale 之基本术语二
表空间 表空间是 Orcale 数据库最大的逻辑结构.表空间就是一个或者多个数据文件的集合.所有的数据文件都被逻辑的存放在表空间中. 一个数据库包括 SYSTEM.SYSAUX和TMP三个默认表空间, ...
- CentOS 6.4 升级python 2.6.6 到 python 2.7.9
1. 查看当前系统python的版本 python -V 2. 下载python软件包 cd /usr/local/src wget http://python.org/ftp/python/2.7. ...
- matlab 子函数的使用
本文参考了该篇博客:http://www.cnblogs.com/MarshallL/p/4048846.html 对其进行学习,为我所用吧. 一. 在matlab的函数定义中,如果函数如果函数较长或 ...
- java 非缓冲与缓冲数据读取比较
首先不适用缓存技术,读取数据: //非缓冲计时 package com.swust; import java.io.*; /* *功能:创建一个程序,写10000个随机双精度的数到一个文件中,同时测试 ...
- Ext Js详解指南
什么是Ext JS 走进Ext的世界 Ext JS是一款富客户端开发框架它基于javascript.HTML和CSS开发而成,无需安装任何插件即可在常用浏览器中创建出绚丽的页面效果. 个人总结Ext ...
- C#表达式和语句
表达式由操作数 (operand) 和运算符 (operator) 构成.表达式的运算符指示对操作数适用什么样的运算.运算符的示例包括+.-.*./ 和 new.操作数的示例包括文本.字段.局部变量和 ...
- Java(多态练习 instanceof)
/* 题目: (多态,instanceof)有如下代码 class Animal { private String name; // 1 } class Dog extends Animal { // ...