iOS 《Quartz 2D编程指南》之常见图形的绘制【 饼图、柱状图、雪花、手势密码、画板】

2021-04-15 16:08:02 浏览数 (1)

引言

原文:

https://kunnan.blog.csdn.net/article/details/113043866

I、 饼图

这里写图片描述

代码语言:javascript复制
/*pie
 
 寻找规律
 
 */
 
// Only override drawRect: if you perform custom drawing.
 
// An empty implementation adversely affects performance during animation.
 
- (void)drawRect:(CGRect)rect {
 
    // Drawing code
 
    CGContextRef context = UIGraphicsGetCurrentContext();
 
    NSArray *data = @[@25,@25,@50];
 
    CGPoint center = CGPointMake(125, 125);
 
    CGFloat radious = 100;//半径
 
    CGFloat startAngle = 0;//起始弧度
 
    CGFloat endAngle = 0;
 
    CGFloat angle = 0;
 
    //开始绘制饼图
 
    for (NSNumber *num in data) {
 
        //绘制
 
        angle =num.floatValue/100.0 *M_PI*2;
 
        startAngle = endAngle;//上一个饼图的结束弧度为下一个饼图的开始弧度
 
        endAngle = startAngle   angle;
 
        UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:center radius:radious startAngle:startAngle endAngle:endAngle clockwise:YES];
 
        [path2 addLineToPoint:center];
 
        CGContextAddPath(context, path2.CGPath);
 
        [[UIColor randomColor]set];
 
        //渲染第i个
 
        CGContextFillPath(context);
 
    }
 
     
 
}
 
 
 
 
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
 
    [self setNeedsDisplay];
 
}
 
//生成随机颜色
  (UIColor *)randomColor{
 
    /*
 
     RGB 24位,RGB 每个颜色通道8位,8 的二进制255,即颜色取值是0~255
 
     RGBA
 
     */
 
    CGFloat red = arc4random_uniform(256)/255.0;
 
    CGFloat blue = arc4random_uniform(256)/255.0;
 
    CGFloat green = arc4random_uniform(256)/255.0;
 
    return  [UIColor colorWithRed:red green:green blue:blue alpha:1];
 
}

II、柱状图

这里写图片描述

0 人点赞