绘制虚线
- (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);
}效果如下:
绘制椭圆与圆
- (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)
CGContextRef c,
CGFloat x, 圆心点坐标的x和y
CGFloat y,
CGFloat radius, 半径
CGFloat startAngle, 绘制起始点的弧度值,一般在IOS绘图里都使用弧度这个概念
int clockwise 1为顺时针,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);
}- (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);
}
8251



被折叠的 条评论
为什么被折叠?



