iOS学习资料程序员iOS Developer

IOS 饼状图简单画法

2016-04-28  本文已影响889人  半白乀

效果


ios饼状图简单画法-效果图

Demo地址

重写 - (void)drawRect:(CGRect)rect { //... }


线宽,用线去涂色!

CGFloat viewHalfWidth = rect.size.width/2.0;

CGFloat lineWidth = viewHalfWidth;

内部想预留空间减去相应长度即可

CGFloat insideCircleWidth = 50;

lineWidth = viewHalfWidth - insideCircleWidth;


半径、中心点

CGFloat circleRadius = viewHalfWidth - lineWidth/2;

CGPoint circleCenterPoint = CGPointMake(viewHalfWidth, viewHalfWidth);


必要的数据

NSArray<UIColor*>*colorArray = [NSArray arrayWithObjects:[UIColor redColor],  [UIColor orangeColor], [UIColor brownColor], [UIColor cyanColor], nil];

NSArray<NSNumber*>*percentArray = [NSArray arrayWithObjects:@(.1), @(.2), @(.3), @(.4), nil];


准备好之后开始画了!

CGContextRef pieViewContext = UIGraphicsGetCurrentContext();

CGFloat startAngel = 0;

CGFloat endAngel;

for (int i =0; i < colorArray.count; i++) {

endAngel = startAngel + M_PI*2*percentArray[i].floatValue;

CGContextAddArc(pieViewContext, circleCenterPoint.x, circleCenterPoint.y, circleRadius, startAngel, endAngel, NO);

CGContextSetStrokeColorWithColor(pieViewContext, colorArray[i].CGColor);

CGContextSetLineWidth(pieViewContext, lineWidth);

CGContextStrokePath(pieViewContext);

startAngel = endAngel;

}

Demo地址


最后,发挥自己的想象,抽离出其中变量,满足自己的具体需求!

上一篇 下一篇

猜你喜欢

热点阅读