iphone上如何繪製餅圖(使用CGContextAddArc)(原創)

來源:互聯網
上載者:User



CGContextAddArc是一個比較強大的函數,建議仔細看一下iphone的開發文檔。

CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, intclockwise)

 

  • CGContextRef: 圖形上下文
  • x,y: 開始畫的座標
  • radius: 半徑
  • startAngle, endAngle: 開始的弧度,結束的弧度
  • clockwise: 畫的方向(順時針,逆時針)
GraphView.h檔案:

 

#import <UIKit/UIKit.h>#import <Foundation/Foundation.h> @interface GraphView : UIView { } @end

 

 

 

GraphView.m檔案:
#import "GraphView.h"#define PI 3.14159265358979323846static inline float radians(double degrees) { return degrees * PI / 180; } @interface GraphView(private)//如果有什麼私人方法,在這裡聲明@end@implementation GraphView- (id)initWithFrame:(CGRect)frame {    if ((self = [super initWithFrame:frame])) {        // Initialization code    }    return self;}- (void)drawRect:(CGRect)rect { CGRect parentViewBounds = self.bounds;CGFloat x = CGRectGetWidth(parentViewBounds)/2;CGFloat y = CGRectGetHeight(parentViewBounds)*0.55;     // Get the graphics context and clear it    CGContextRef ctx = UIGraphicsGetCurrentContext();    CGContextClearRect(ctx, rect); // define stroke colorCGContextSetRGBStrokeColor(ctx, 1, 1, 1, 1.0); // define line widthCGContextSetLineWidth(ctx, 4.0);         // need some values to draw pie charts         double snapshotCapacity =20;        double rawCapacity = 100;        double systemCapacity = 1; int offset = 5;double pie1_start = 315.0;double pie1_finish = snapshotCapacity *360.0/rawCapacity; double system_finish = systemCapacity*360.0/rawCapacity;     CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor greenColor] CGColor]));    CGContextMoveToPoint(ctx, x+2*offset, y);         CGContextAddArc(ctx, x+2*offset, y, 100,  radians(snapshot_start), radians(snapshot_start+snapshot_finish), 0);     CGContextClosePath(ctx);     CGContextFillPath(ctx);  // system capacity CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor colorWithRed:15 green:165/255 blue:0 alpha:1 ] CGColor]));CGContextMoveToPoint(ctx, x+offset,y);         CGContextAddArc(ctx, x+offset, y, 100,  radians(snapshot_start+snapshot_finish+offset), radians(snapshot_start+snapshot_finish+system_finish), 0);     CGContextClosePath(ctx);     CGContextFillPath(ctx);  /* data capacity */CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor colorWithRed:99/255 green:184/255 blue:255/255 alpha:1 ] CGColor]));CGContextMoveToPoint(ctx, x, y);         CGContextAddArc(ctx, x, y, 100,  radians(snapshot_start+snapshot_finish+system_finish+offset), radians(snapshot_start), 0);     CGContextClosePath(ctx);     CGContextFillPath(ctx);}- (void)dealloc {    [super dealloc];}@end


調用代碼如下:
GraphView* viewTest = [[GraphView alloc] initWithFrame:CGRectMake(x,y,width,height)];
[self.view addsubView:viewTest];
[viewTest release];


如下:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.