日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iphone上如何绘制饼图(使用CGContextAddArc)(原创)

發(fā)布時(shí)間:2023/12/19 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iphone上如何绘制饼图(使用CGContextAddArc)(原创) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.



CGContextAddArc是一個(gè)比較強(qiáng)大的函數(shù),建議仔細(xì)看一下iphone的開發(fā)文檔。

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

?

  • CGContextRef: 圖形上下文
  • x,y: 開始畫的坐標(biāo)
  • radius: 半徑
  • startAngle, endAngle: 開始的弧度,結(jié)束的弧度
  • clockwise: 畫的方向(順時(shí)針,逆時(shí)針)
GraphView.h文件:

?

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

?

?

?

GraphView.m文件: #import "GraphView.h" #define PI 3.14159265358979323846 static 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 itCGContextRef 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 chartsdouble 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

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

效果圖如下:

轉(zhuǎn)載于:https://www.cnblogs.com/moshengren/archive/2010/10/19/1855246.html

總結(jié)

以上是生活随笔為你收集整理的iphone上如何绘制饼图(使用CGContextAddArc)(原创)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。