-(void)drawRect:(CGRect)rect{
//[self drawline ];
//[self drawball];
//[self drawxunLine];
//[self drawxuboll];
//绘制上下文方式画一条线
[self drawlineGrap];
}
-(void)drawxuboll{
//1 获取上下文
CGContextRef ref = UIGraphicsGetCurrentContext();
//路径
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 50, 90)];
//颜色边线
[[UIColor redColor]setStroke];
//填充颜色
[[UIColor yellowColor]setFill];
//线宽
CGContextSetLineWidth(ref, 4);
//虚线
CGFloat lengths[] = {7,11};
CGContextSetLineDash(ref, 0, lengths, 2);
//将路径添加到上下文中
CGContextAddPath(ref, path.CGPath);
//end:将上下文渲染
CGContextDrawPath(ref, kCGPathFillStroke);
}
-(void)drawxunLine{
//另外一中画横线的方法
//创建上下文
CGContextRef ref = UIGraphicsGetCurrentContext();
//创建路径
UIBezierPath *path1 = [UIBezierPath bezierPath];
//创建七点
[path1 moveToPoint:CGPointMake(1, 0)];
//增加一条线段到某点
[path1 addLineToPoint:CGPointMake(99, 99)];
//颜色
[[UIColor redColor]set];
//线宽
CGContextSetLineWidth(ref, 4);
//虚线
CGFloat lengths[] = {4,2};
CGContextSetLineDash(ref, 0, lengths, 2);
//将路径关联到上下文
CGContextAddPath(ref, path1.CGPath);
//end : 渲染
// CGContextStrokePath(ref); //stroke
CGContextDrawPath(ref, kCGPathFillStroke);//
}
-(void)drawball{
//宽高相等为圆形 , 宽高不相等为 椭圆形
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(88, 0, 88, 108)];
///颜色set = fill+stroke 同时执行
// [[UIColor redColor]set];
// stroke绘制外边框的颜色
[[UIColor redColor]setStroke];
//fill 绘制图形里边的填充颜色
[[UIColor yellowColor]setFill];;
path.lineWidth = 5;
//绘制
[path stroke];
[path fill];
}
-(void)drawline{
//绘制一条横线
//用贝塞尔曲线类(路径)
UIBezierPath *path =[UIBezierPath bezierPath];
//创建起点坐标
[path moveToPoint:CGPointMake(0, 0)];
//增加一条线段到某个点
[path addLineToPoint:CGPointMake(33, 33)];
//设置先段的颜色
[[UIColor redColor] set];
//设置线宽
path.lineWidth =4;
//end: 绘制上去
[path stroke];
UIBezierPath *path1 = [UIBezierPath bezierPath];
[path1 moveToPoint:CGPointMake(20, 20)];
[path1 addLineToPoint:CGPointMake(100, 100)];
[path1 addLineToPoint:CGPointMake(40, 120)];
[path1 closePath];
[[UIColor orangeColor]set];
path1.lineWidth = 10;
[path1 stroke];
}
-(void)drawlineGrap{
CGContextRef ref = UIGraphicsGetCurrentContext();
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(30, 30)];
[[UIColor blueColor]set];
CGContextSetLineWidth(ref, 4);
CGContextAddPath(ref, path.CGPath);
CGContextDrawPath(ref, kCGPathFillStroke);
}