IOS 饼状图简单画法

效果


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地址


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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • #define kBlackColor [UIColor blackColor] //.h //划线 + (voi...
    CHADHEA阅读 816评论 0 1
  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,143评论 1 6
  • #import "ChangeAnimationView.h" ``` @interface ChangeAnim...
    JinHuiZhang阅读 603评论 0 0
  • Quartz2D以及drawRect的重绘机制字数1487 阅读21 评论1 喜欢1一、什么是Quartz2D Q...
    PurpleWind阅读 804评论 0 3
  • 晨曦 柔和的阳光 想要怒放的生命 这组图是十一回家帮父母农忙时拍的。在农村,下地劳作都赶早,所以那天我们五点就下地...
    我的老师是只猫阅读 449评论 1 7