绘制虚线

虚线绘制主要调用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. swing入门例子

    // a simple exmple that can show the basis of swing------------------------------------------------- ...

  2. Java八个并发学习——线程同步工具CyclicBarrier

    本文是一篇文章对网络的研究摘要,感谢您的无私分享. CyclicBarrier 类有一个整数初始值,此值表示将在同一点同步的线程数量.当当中一个线程到达确定点,它会调用await() 方法来等待其它线 ...

  3. Bloom Filter 算法具体解释

    Bloom Filter 算法 Bloom filter是由Burton Bloom 在1970年提出的,其后在P2P上得到了广泛的应用.Bloom filter 算法可用来查询某一数据是否在某一数据 ...

  4. C++11 多线程 基础

    C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用 ...

  5. Hacker(18)----了解Windows系统漏洞

    一.WinXP中的漏洞 在WinXP中,常见的漏洞主要有UPNP服务漏洞.帮助与支持中心漏洞.压缩文件夹漏洞.服务拒绝漏洞.RDP漏洞以及热键漏洞. 1.UPNP服务漏洞 漏洞描述:UPNP(Univ ...

  6. 代码,显示IPhone剩余磁盘空间

    #include <sys/mount.h> //这段代码示范怎么取得 iPhone 的剩余磁盘空间,还有全部磁盘空间 long long freeSpace() { struct sta ...

  7. Sublime Text3 Package Control和Emmet插件安装方法

    因为初学前端,所以今天安装了Sumblime Text 3,然后就停不下来去找Package Control的安装方法. 网络上我找到并尝试过的方法有两种,我使用的是用Python代码去安装并成安装成 ...

  8. SQL Server链接MySQL实践

    最近在访问多数据库的时候进行了SQLServer链接MySQL数据的实践,现总结如下: 一.  安装mysql-connector-odbc驱动: 1. 在SQL Server服务器的机器上安装mys ...

  9. hbase 单机模式安装

    1:下载安装包(我下载的0.94版本,如果考虑后期与hadoop兼容,需要找合适的版本) http://mirrors.hust.edu.cn/apache/hbase/hbase-0.94.20/h ...

  10. linux shell脚本连接oracle查询数据插入文件和日志文件中

    #!/bin/sh sqlplus "用户名/密码@数据库"<<EOF  或者只有一个库的 :sqlplus "用户名/密码"<<EOF ...