#import "DrawView.h"
@implementation DrawView
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 300)];
[path addLineToPoint:CGPointMake(300, 300)];
[path moveToPoint:CGPointMake(0, 300)];
[path addLineToPoint:CGPointMake(0, 0)];
path.lineWidth = 5;
[path stroke];
int line = 0;
NSArray *dataArr = @[@100,@200,@200,@250,@10,@20];
for (NSNumber *num in dataArr) {
CGPoint startPoint = CGPointMake(30 + line * 300/dataArr.count, 300);
CGPoint endPoint = CGPointMake(30 + line * 300/dataArr.count, 300 - 300 * (num.intValue / 300.0));
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:startPoint];
[path addLineToPoint:endPoint];
path.lineWidth = 20;
[path stroke];
line ++;
}
}
-(void)shape1{
//方形
// UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 200, 200)];
// //填充
// [path fill];
//椭圆
// UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 200, 200)];
// [path stroke];
//圆角
// UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:150];
// [path stroke];
//圆(扇形) startAngle是从三点方向开始画 clockwise是顺时针
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:M_PI_2 clockwise:YES];
[path addLineToPoint:CGPointMake(150, 150)];
[path closePath];
[path stroke];
UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:M_PI_2 endAngle:M_PI_2 + M_PI_2 clockwise:YES];
[[UIColor redColor] set];
[path2 addLineToPoint:CGPointMake(150, 150)];
[path2 closePath];
[path2 stroke];
UIBezierPath *path3 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:M_PI_2 *2 endAngle:M_PI_2 * 3 clockwise:YES];
[[UIColor yellowColor] set];
[path3 addLineToPoint:CGPointMake(150, 150)];
[path3 closePath];
[path3 stroke];
UIBezierPath *path4 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:M_PI_2 *3 endAngle:M_PI_2 * 4 clockwise:YES];
[[UIColor orangeColor] set];
[path4 addLineToPoint:CGPointMake(150, 150)];
[path4 closePath];
[path4 stroke];
}
- (void)shape2{
NSArray *dataArray = @[@20,@10,@10,@10,@10,@20,@20];
//记录起点和终点
CGFloat start = 0;
CGFloat end = 0;
// 遍历数据
for (NSNumber *num in dataArray) {
end = num.floatValue / 100 *M_PI *2;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:start endAngle:start + end clockwise:YES];
//关闭路径
[path addLineToPoint:CGPointMake(150, 150)];
[path closePath];
[[UIColor colorWithRed:arc4random()%256 / 255.0 green:arc4random()%256 / 255.0 blue:arc4random()%256 / 255.0 alpha:1] set];
[path fill];
start += end;
}
}
@end