CGContextAddArc是一个比较强大的函数,建议仔细看一下iphone的开发文档。
CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, intclockwise)
- CGContextRef: 图形上下文
- x,y: 开始画的坐标
- radius: 半径
- startAngle, endAngle: 开始的弧度,结束的弧度
- clockwise: 画的方向(顺时针,逆时针)
1 |
<pre class = "brush:objc" >GraphView.h文件:</pre> |
1 |
#import <UIKit/UIKit.h> |
2 |
#import <Foundation/Foundation.h> |
4 |
@interface GraphView : UIView { |
01 |
<pre class = "brush:objc" >GraphView.m文件:</pre> |
02 |
<pre class = "brush:objc" ><div class = "cnblogs_Highlighter" ><pre class = "brush:objc" >#import "GraphView.h" |
03 |
#define PI 3.14159265358979323846 |
04 |
static inline float radians( double degrees) { return degrees * PI / 180; } |
07 |
@interface GraphView( private ) |
11 |
@implementation GraphView |
13 |
- ( id )initWithFrame:(CGRect)frame { |
14 |
if (( self = [ super initWithFrame:frame])) { |
20 |
- ( void )drawRect:(CGRect)rect { |
22 |
CGRect parentViewBounds = self .bounds; |
23 |
CGFloat x = CGRectGetWidth(parentViewBounds)/2; |
24 |
CGFloat y = CGRectGetHeight(parentViewBounds)*0.55; |
27 |
CGContextRef ctx = UIGraphicsGetCurrentContext(); |
28 |
CGContextClearRect(ctx, rect); |
31 |
CGContextSetRGBStrokeColor(ctx, 1, 1, 1, 1.0); |
34 |
CGContextSetLineWidth(ctx, 4.0); |
39 |
double snapshotCapacity =20; |
40 |
double rawCapacity = 100; |
41 |
double systemCapacity = 1; |
44 |
double pie1_start = 315.0; |
45 |
double pie1_finish = snapshotCapacity *360.0/rawCapacity; |
46 |
double system_finish = systemCapacity*360.0/rawCapacity; |
48 |
CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor greenColor] CGColor])); |
49 |
CGContextMoveToPoint(ctx, x+2*offset, y); |
50 |
CGContextAddArc(ctx, x+2*offset, y, 100, radians(snapshot_start), radians(snapshot_start+snapshot_finish), 0); |
51 |
CGContextClosePath(ctx); |
52 |
CGContextFillPath(ctx); |
55 |
CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor colorWithRed:15 green:165/255 blue:0 alpha:1 ] CGColor])); |
56 |
CGContextMoveToPoint(ctx, x+offset,y); |
57 |
CGContextAddArc(ctx, x+offset, y, 100, radians(snapshot_start+snapshot_finish+offset), radians(snapshot_start+snapshot_finish+system_finish), 0); |
58 |
CGContextClosePath(ctx); |
59 |
CGContextFillPath(ctx); |
62 |
CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor colorWithRed:99/255 green:184/255 blue:255/255 alpha:1 ] CGColor])); |
63 |
CGContextMoveToPoint(ctx, x, y); |
64 |
CGContextAddArc(ctx, x, y, 100, radians(snapshot_start+snapshot_finish+system_finish+offset), radians(snapshot_start), 0); |
65 |
CGContextClosePath(ctx); |
66 |
CGContextFillPath(ctx); |
78 |
<pre class = "brush:objc" ><br></pre> |
79 |
<pre class = "brush:objc" >调用代码如下:</pre> |
80 |
<pre class = "brush:objc" ><pre class = "brush:objc" >GraphView* viewTest = [[GraphView alloc] initWithFrame:CGRectMake(x,y,width,height)];</pre> |
81 |
<pre class = "brush:objc" >[ self .view addsubView:viewTest];</pre> |
82 |
<pre class = "brush:objc" >[viewTest release];</pre> |
83 |
<pre class = "brush:objc" ><br></pre> |
84 |
<pre class = "brush:objc" ><br></pre> |
85 |
<pre class = "brush:objc" >效果图如下:</pre> |
http://www.cnblogs.com/moshengren/archive/2010/10/19/1855246.html