您的位置:首页 > 产品设计 > UI/UE

Quartz2D - 04.利用贝瑟尔路径(UIBezierPath)绘制基本图形

2015-08-05 21:14 423 查看

1.基本形状

// 自定义View
#import "DrawView.h"
@implementation DrawView
// 实现drawRect方法绘图
-(void)drawRect:(CGRect)rect{
//  画线
//    [self drawLine1];
//    [self drawLine2];
// 画曲线
//    [self drawQuadCurve];
// 画形状
//    [self drawRect];
// 画圆
//    [self drawCircle];
// 画扇形
//    [self drawFan];
// 画圆环
[self drawLoop];
}

// 画圆环
-(void)drawLoop
{
// 创建贝瑟尔路径
UIBezierPath *path = [UIBezierPath bezierPath];

// 设定圆心
CGPoint center = CGPointMake(100, 100);

// 画弧
[path addArcWithCenter:center radius:50 startAngle:0 endAngle:M_PI * 2 clockwise:YES];

// 画线
[path addLineToPoint:center];

// 关闭路径
[path closePath];

// 设置线宽
path.lineWidth = 10;
// 设置画笔颜色
[[UIColor greenColor] setStroke];
// 路径走线
[path stroke];

// 设置填充颜色
[[UIColor redColor] setFill];
// 填充路径
[path fill];

}

// 画扇形
-(void)drawFan{

// 创建贝瑟尔路径
UIBezierPath *path = [UIBezierPath bezierPath];

CGPoint center = CGPointMake(100, 100);
// 画弧
[path addArcWithCenter:center radius:50 startAngle:0 endAngle:M_PI_2 clockwise:YES];
// 画线
[path addLineToPoint:center];

// 关闭路径
[path closePath];

// 设置画笔颜色
[[UIColor redColor] set];
// 画路径
[path fill];

}

// 画圆形
-(void)drawCircle{
// 创建贝瑟尔路径
UIBezierPath *path = [UIBezierPath bezierPath];

// 画线
[path addArcWithCenter:CGPointMake(100, 100) radius:50 startAngle:0 endAngle:M_PI * 2 clockwise:YES];

// 设置画笔颜色
[[UIColor redColor] setStroke];
// 画路径
[path stroke];

}

// 画矩形
-(void)drawRect{

// 创建贝瑟尔路径
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, 100, 100)];

// 设置属性
// 设置线宽
path.lineWidth = 20;
// 设置交点样式
path.lineJoinStyle = kCGLineJoinRound;
// 设置画笔颜色
[[UIColor redColor] setStroke];
// 画路径
[path stroke];

}

// 画曲线
-(void)drawQuadCurve{
// 创建贝瑟尔路径
UIBezierPath *path = [UIBezierPath bezierPath];

// 画曲线
// 设置起始点
[path moveToPoint:CGPointMake(10, 100)];
// 画曲线,根据控制点和终点
[path addQuadCurveToPoint:CGPointMake(110, 100) controlPoint:CGPointMake(50, 10)];

// 渲染路径
[path stroke];
}

// 画线1
-(void)drawLine1{
// 获取图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();

// 创建贝瑟尔路径
UIBezierPath *path = [UIBezierPath bezierPath];

// 移动到初始点
[path moveToPoint:CGPointZero];

// 画线
[path addLineToPoint:CGPointMake(100, 100)];

// 设置线宽
CGContextSetLineWidth(ctx, 10);

// 添加路径
CGContextAddPath(ctx, path.CGPath);

// 渲染视图
CGContextStrokePath(ctx);
}
// 画线2
-(void)drawLine2{

// 创建贝瑟尔路径
UIBezierPath *path = [UIBezierPath bezierPath];
// 移动到初始点
[path moveToPoint:CGPointZero];

// 画线
[path addLineToPoint:CGPointMake(100, 100)];

// 设置线宽
path.lineWidth = 20;

// 设置圆角
path.lineCapStyle = kCGLineCapRound;

// 渲染界面
[path stroke];

}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: