//画弧
-(void)drawOval:(CGRect)rect{
//Center:圆心
//radius:圆的半径
//startAngle:开始角度
//endAngle:截止角度
//clockwise:YES顺时针 NO:逆时针
CGPointcenter =CGPointMake(rect.size.width*0.5, rect.size.height*0.5);
CGFloatradius = rect.size.width*0.5-10;
//不能直接使用self.center,是因为self.center是相对于它的父控件
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:M_PI_4 clockwise:YES];
//添加一根线到圆心
[pathaddLineToPoint:center];
//关闭路径 closePath:从路径的终点连接一根线到路径的起点
[pathclosePath];
[[UIColor redColor] set];
//画扇形
//fill(填充之前,会自动关闭路径)
[pathfill];
}
//画圆
-(void)drawRound{
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 100)];
// [path moveToPoint:CGPointMake(50, 250)];
// [path addLineToPoint:CGPointMake(250, 250)];
//UIBezierPath提供商的绘图方法进行绘制
//获取上下文-->绘制路径-->把路径添加到上下文-->把上下文内容渲染到View上,只有在drawRect:方法当中才能获取上下文,在awakeFromNib当中获取不到上下文,所以没有办法进行绘图
[pathstroke];
}
//画矩形
-(void)drawRoundedRect{
//1、获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2、绘制路径
//矩形
UIBezierPath *path1 = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 100, 100)];
//圆角矩形
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 100, 100) cornerRadius:5];
[[UIColor redColor] set];
//3、把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4、把上下文内容渲染到View上
//CGContextStrokePath(ctx);
CGContextFillPath(ctx);
}
网友评论