绘制虚线

虚线绘制主要调用CGContextSetLineDash函数。
这个函数有4个参数,除了一个是上下文外,phase为初始跳过几个点开始绘制,第三个参数为一个CGFloat数组,指定你绘制的样式,绘几个点跳几个点(下面为绘10个点,跳过5个),最后一个参数是上个参数数组元素的个数。

- (void)drawLineDash
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetLineWidth(context, 2.0f);
CGContextSetLineDash(context, 0, (CGFloat[]){10, 5}, 2);//绘制10个跳过5个
CGContextSetStrokeColorWithColor(context, [[UIColor brownColor] CGColor]);
CGContextMoveToPoint(context, 0, 20);
CGContextAddLineToPoint(context, 320, 20);
CGContextStrokePath(context);
CGContextRestoreGState(context);
}

效果如下:





绘制椭圆与圆


CGContextAddEllipseInRect函数
函数比较简单,只需要上下文和一个矩形参数。默认系统会绘制填充这个矩形内部的最大椭圆,若矩形为正方形,则为圆。

- (void)drawEllipse
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextAddEllipseInRect(context, CGRectMake(40, 180, 240, 120));
CGContextSetLineWidth(context, 3.0f);
CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]);
CGContextStrokePath(context);
CGContextRestoreGState(context);
}

效果





绘制弧,饼图(Pie Chart)


CGContextAddArc函数是一个画弧的函数,使用并不难,与其他一样,按照参数要求给其赋值就可以了,而且这个函数也十分好用,可以借助其实现绘制扇形,绘制饼图。
简单的看下这个函数
void CGContextAddArc (
CGContextRef c,
CGFloat x,圆心点坐标的x和y
CGFloat y,
CGFloat radius,半径
CGFloat startAngle,绘制起始点的弧度值,一般在IOS绘图里都使用弧度这个概念
CGFloat endAngle,绘制终点的弧度值
int clockwise1为顺时针,0为逆时针。
);

首先为了方便,我写了一个绘制饼图各个部分的函数。

void drawPieChart(CGContextRef context, CGPoint point, float start_angel, float end_angle, double radius, CGColorRef color)
{
CGContextMoveToPoint(context, point.x, point.y);
CGContextSetFillColorWithColor(context, color);
CGContextAddArc(context, point.x, point.y, radius, start_angel, end_angle, 0);
CGContextFillPath(context);
}

然后进行绘制

#define RADIANS(x) ((x)*(M_PI)/180)获取弧度

- (void)drawRect:(CGRect)rect
{
// Drawing code
// [self drawLineDash];
// [self drawEllipse]; CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetShadow(context, CGSizeMake(0, 10), 10); //简单的设置了一个阴影
double radius = 120; float start = RADIANS(0);
float end = RADIANS(120);
drawPieChart(context, self.center, start, end, radius, [[UIColor orangeColor] CGColor]); start = end;
end = RADIANS(194);
drawPieChart(context, self.center, start, end, radius, [[UIColor yellowColor] CGColor]); start = end;
end = RADIANS(231);
drawPieChart(context, self.center, start, end, radius, [[UIColor purpleColor] CGColor]); start = end;
end = RADIANS(360);
drawPieChart(context, self.center, start, end, radius, [[UIColor blueColor] CGColor]); CGContextRestoreGState(context);
}

效果




以上为本篇博客全部内容,欢迎指正和交流。如需转载请注明出处~

UIKit和Core Graphics绘图(三)——绘制虚线,椭圆以及饼图的更多相关文章

  1. UIKit和Core Graphics绘图(一)——字符串,线条,矩形,渐变

    概述 CoreGraphics也称为Quartz 2D 是UIKit下的主要绘图系统,频繁的用于绘制自定义视图.Core Graphics是高度集成于UIView和其他UIKit部分的.Core Gr ...

  2. iOS绘图系统UIKit与Core Graphics

    概述 iOS主要的绘图系统有UIKit,Core Graphics,Core Animation,Core Image,Open GL等,本片博文主要介绍UIKit与Core Graphics的绘图系 ...

  3. iOS 使用drawRect: 绘制虚线椭圆

    iOS 使用drawRect: 绘制虚线椭圆 1:首先如果要使用 drawRect 绘图 要导入 CoreGraphics.framework 框架 然后 创建 自定义view, 即是 myView继 ...

  4. iOS实现图形编程可以使用三种API(UIKIT、Core Graphics、OpenGL ES及GLKit)

    这些api包含的绘制操作都在一个图形环境中进行绘制.一个图形环境包含绘制参数和所有的绘制需要的设备特定信息,包括屏幕图形环境.offscreen 位图环境和PDF图形环境,用来在屏幕表面.一个位图或一 ...

  5. Core Graphics绘图

    首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Context ...

  6. Cocoa Touch(三):图形界面UIKit、Core Animation、Core Graphics

    UIKit 视图树模型 1.视图树模型 计算机图形实际上是一个视图树模型,每个视图都有一个本地坐标系.每个本地坐标系的组成部分是:原点在父坐标系中的位置,每个基在父坐标系中的位置,由此就可以根据向量的 ...

  7. Core Graphics框架 利用Quartz 2D绘图

    首先,什么是Core Graphics和Quartz 2D? Core Graphics:是基于Quartz 2D绘图引擎的一个C语言的API绘图框架.它也是iOS开发中最基本的框架(Framewor ...

  8. iOS 图形处理 Core Graphics Quartz2D 教程

    Core Graphics Framework是一套基于C的API框架,使用了Quartz作为绘图引擎.它提供了低级别.轻量级.高保真度的2D渲染.该框架可以用于基于路径的 绘图.变换.颜色管理.脱屏 ...

  9. Core Graphics框架

    在iOS中常用的框架是Quartz 2D,它是Core Graphics框架的一部分,是一个强大的二维图像绘制引擎.我们日常开发所用到的UIKit的组件都是由Core Graphics框架进行绘制的. ...

随机推荐

  1. 高性能MySql进化论【转】

    高性能MySql进化论(十二):Mysql中分区表的使用总结 http://binary.duapp.com/category/sql 当数据量非常大时(表的容量到达GB或者是TB),如果仍然采用索引 ...

  2. Android万能分辨率适应法

    (1)获取屏幕的尺寸 WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); D ...

  3. hdu-1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10219   Accepted: 4977 Des ...

  4. C++11 静态断言(static_assert)

      简介 C++0x中引入了static_assert这个关键字,用来做编译期间的断言,因此叫做静态断言. 其语法很简单:static_assert(常量表达式,提示字符串). 如果第一个参数常量表达 ...

  5. 命令行下如何安装VMware Tools并与windows资料共享

    安装VMware Tools: 选择菜单栏“虚拟机”——“安装VMware tools” ,等待系统自动更换ISO光盘 mount /dev/cdrom /mntcd /mnttar zxvf VMw ...

  6. vim搜索后跳到下(上)一个

    搜索高亮后, 跳到下一个:小写n 上一个:大写N

  7. Merge Sorted Array 合并数组并排序

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  8. 用pod导入ReactiveCocoa

    用pod导入ReactiveCocoa [1]   第一种 platform:ios,'7.0' pod 'ReactiveCocoa' [2]  第二种 pod 'ReactiveCocoa','2 ...

  9. web开发常用样式

    1.div保持底部浮动(不受滚动条影响) position:fixed;_position:absolute;bottom:0px;_bottom:0px;_margin-top:expression ...

  10. c#中将默认常量(32bit)转换为8bit

    // //将int进制转换 // private byte hex(int myHex) { byte[] a = BitConverter.GetBytes(myHex); return a[0]; ...