美文网首页iOS Developer
Core Graphics 绘图

Core Graphics 绘图

作者: 乱尘 | 来源:发表于2017-04-14 14:07 被阅读45次

// 获取当前图形,视图推入堆栈的图形,相当于你所要绘制图形的图纸
CGContextRef ctx = UIGraphicsGetCurrentContext();

// 在当前路径下添加一个矩形路径
CGContextAddRect(ctx, rect);

// 在当前路径下添加一个椭圆路径
CGContextAddEllipseInRect(ctx, rect);
Pasted Graphic.png
// 在当前路径下添加一个曲线路径
/**
 *  @先指定一个起点
 *  @brief 在指定点A追加二次贝塞尔曲线,通过控制点和结束点指定曲线。
 *         关于曲线的点的控制见下图说明,图片来源苹果官方网站。参数按顺序说明
 *  @param ctx   当前图形
 *  @param x 曲线控制点B的x坐标
 *  @param y 曲线控制点B的y坐标
 *  @param x   指定点C的x坐标值
 *  @param y   指定点C的y坐标值

 *

 */
CGContextMoveToPoint(ctx, 10, 10);
CGContextAddQuadCurveToPoint(ctx, 160, 50, 190, 50);
page3image376.png
/**
 *  @brief 在当前路径添加圆弧 参数按顺序说明
 *
 *  @param c           当前图形
 *  @param x           圆弧的中心点坐标x
 *  @param y           曲线控制点的y坐标
 *  @param radius      指定点的x坐标值
 *  @param startAngle  弧的起点与正X轴的夹角,
 *  @param endAngle    弧的终点与正X轴的夹角
 *  @param clockwise   指定1创建一个顺时针的圆弧,或是指定0创建一个逆时针圆弧
 *
 */
CGContextAddArc(ctx, x, y, 20, 0, 2 * M_PI, 1);


// 设置图形的线宽
CGContextSetLineWidth(ctx, 20);

// 设置视图的当前填充色
CGContextSetFillColorWithColor(ctx, [UIColor blackColor].CGColor); 
//或者 [[UIColor redColor] setFill];

// 设置视图的当前描边色
CGContextSetStrokeColorWithColor(ctx, [UIColor brownColor].CGColor);
 //或者 [[UIColor redColor] setStroke];

// 根据当前路径,宽度及颜色绘制线
CGContextStrokePath(ctx);

//填充当前路径区域
CGContextFillPath(ctx);

 //CGContextDrawPath(ctx, kCGPathStroke);  填充/描线
 //kCGPathFill填充非零绕数规则,kCGPathEOFill表示用奇偶规则,kCGPathStroke路径,kCGPathFillStroke路径填充,kCGPathEOFillStroke表示描线,不是填充  

CGContextSetLineDash
此函数需要四个参数:

  • context – 这个不用多说
  • phase - 第一次跳过多少个点
  • lengths – 指明虚线是如何交替绘制,具体看例子
  • count – lengths数组的长度 (读取lengths的长度)

CGFloat arr[] = {2,1,3};
CGContextSetLineDash(context, 0, arr,2);

/*画贝塞尔曲线*/
//二次曲线
CGContextMoveToPoint(context, 120, 300);//设置Path的起点
CGContextAddQuadCurveToPoint(context,190, 310, 120, 390);//设置贝塞尔曲线的控制点坐标和终点坐标
CGContextStrokePath(context);
//三次曲线函数
CGContextMoveToPoint(context, 200, 300);//设置Path的起点
CGContextAddCurveToPoint(context,250, 280, 250, 400, 280, 300);//设置贝塞尔曲线的控制点坐标和控制点坐标终点坐标
CGContextStrokePath(context);

绘图实例:

Paste_Image.png

DrawView.m :

Paste_Image.png Paste_Image.png Paste_Image.png
Paste_Image.png Paste_Image.png Paste_Image.png
Paste_Image.png Paste_Image.png Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png

相关文章

  • iOS绘图

    介绍### 说到iOS的绘图肯定就是Core Graphics。 Core Graphics Framework是...

  • Core Graphics Tutorial 总结&翻译

    原文Core Graphics Tutorial Github Core Graphics 是苹果内的矢量绘图框架...

  • iOS绘图功能(一)

    不同的绘图系统### iOS主要的绘图系统有UIKit,Core Graphics(Quartz), Core A...

  • iOS 绘图

    转自:iOS绘图—— UIBezierPath 和 Core Graphics绘图进阶请参考:绘图 前言 iOS系...

  • iOS 札记2:Core Graphics小记

    导语:Core Graphics是iOS和Mac OS X的2D绘图引擎,本文简单介绍Core Graphics绘...

  • iOS中的绘图教程品读后的顿悟篇(二)

    UIKit框架和Core Graphics 作为初学者,很容易被UIKit和Core Graphics两个支持绘图...

  • iOS 中各种图形的绘制

    绘图我们要用到Core Graphics框架,那么什么是Core Graphics框架?首先我们来介绍一下。 一、...

  • Core Graphics绘图

    Core Graphics入门IOS中使用Quartz 2D绘制虚线 Core Graphics与OpenGL的关...

  • 绘图-Core Graphics

    前言 CGContext又叫图形上下文,相当于一块画布,以堆栈形式存放,只有在当前context上绘图才有效。iO...

  • Core Graphics 绘图

    // 获取当前图形,视图推入堆栈的图形,相当于你所要绘制图形的图纸CGContextRef ctx = UIGra...

网友评论

    本文标题: Core Graphics 绘图

    本文链接:https://www.haomeiwen.com/subject/ejoiattx.html