`
jsntghf
  • 浏览: 2468615 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

画饼图

    博客分类:
  • iOS
阅读更多

首先,建立一个用来画饼图的视图。

 

头文件:

 

#import <Foundation/Foundation.h>

@interface PieView : UIView {
	
}

@end

 

实现文件:

 

#import "PieView.h"

@implementation PieView

#define PI 3.14159265358979323846
#define radius 100

static inline float radians(double degrees) { 
	return degrees * PI / 180; 
}

static inline void drawArc(CGContextRef ctx, CGPoint point, float angle_start, float angle_end, UIColor * color) {
	CGContextMoveToPoint(ctx, point.x, point.y);
	CGContextSetFillColor(ctx, CGColorGetComponents([color CGColor]));    
	CGContextAddArc(ctx, point.x, point.y, radius, angle_start, angle_end, 0);
	CGContextFillPath(ctx); 
}

- (void)drawRect:(CGRect)rect {
	CGContextRef ctx = UIGraphicsGetCurrentContext();
	CGContextClearRect(ctx, rect);
	
	float angle_start = radians(0.0);
	float angle_end = radians(121.0);        
	drawArc(ctx, self.center, angle_start, angle_end, [UIColor yellowColor]);
	
	angle_start = angle_end;
	angle_end = radians(228.0);        
	drawArc(ctx, self.center, angle_start, angle_end, [UIColor greenColor]);
	
	angle_start = angle_end;
	angle_end = radians(260);
	drawArc(ctx, self.center, angle_start, angle_end, [UIColor orangeColor]);
	
	angle_start = angle_end;
	angle_end = radians(360);
	drawArc(ctx, self.center, angle_start, angle_end, [UIColor purpleColor]);
}

@end

 

然后,将刚建立的视图加到当前视图上即可。

 

- (void)viewDidLoad {
	[super viewDidLoad];
	PieView *pie = [[PieView alloc] init];
	pie.frame = [[UIScreen mainScreen] bounds];
	[self.view addSubview:(UIView *) pie];
	[pie release];
}

 

示例图:


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics