IOS开发基础之绘制饼图、柱状图、自定义进度条
源码在我的主页里
1.绘制饼图
效果
源码
// LJView.m
// 34-绘图饼图
// Created by 鲁军 on 2021/2/23.
#import "LJView.h"
@implementation LJView
- (void)drawRect:(CGRect)rect {
NSArray *array = @[@0.3,@0.1,@0.2,@0.4];
CGFloat end=0,start = 0;
for(int i=0;i<array.count;++i){
end =2*M_PI* [array[i] floatValue] + start;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:start endAngle:end clockwise:1];
[path addLineToPoint:CGPointMake(150, 150)];
//随机的颜色
[[UIColor colorWithRed:((float)arc4random_uniform(256)/255.0) green:((float)arc4random_uniform(256)/255.0) blue:((float)arc4random_uniform(256)/255.0) alpha:1.0] set];
[path fill];
//下一次的起点等于上一次的终点
start = end;
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// NSLog(@"123");
// [self setNeedsDisplay];
//刷新指定的区域
[self setNeedsDisplayInRect:CGRectMake(0, 0, 150, 150