一、UIBezierPath扫盲
2017-01-05 本文已影响148人
iLees
简述
iOS系统提供了两套绘图框架:<strong>UIBezierPath</strong> 和 <strong>CoreGraphics</strong>, 前者属于UIKit是对后者关于Path的进一步封装。所以后者接近底层更加强大,而前者更加便于使用。由于UIBezierPath已经可以满足我们对绘图的一些基本要求,所以我们可以重点研究它。
使用:
UIBezierPath的使用很简单,有以下三步:
1、创建path;
2、添加路径到path;
3、将path进行绘制。
No code no bibi
下面我们使用三步方式创建几个图形:
1、画直线

- (void)drawRect:(CGRect)rect {
// 1、创建path
UIBezierPath *path = [UIBezierPath bezierPath];
// 2、添加路径到path
[path moveToPoint:CGPointMake(150, 100)];
[path addLineToPoint:CGPointMake(250, 100)];
// 3、将path绘制出来
[path stroke];
}
2、画圆

- (void)drawRect:(CGRect)rect {
// 1、创建path
UIBezierPath *path = [UIBezierPath bezierPath];
// 2、添加 Arc(就是弧度的意思嘛) 路径
[path addArcWithCenter:CGPointMake(200, 150) radius:60.0f startAngle:0.0f endAngle:180.0f clockwise:YES];
// 3、绘制path
[path stroke];
}
3、画一个椭圆形(有填充色和描边)

- (void)drawRect:(CGRect)rect {
// 1、创建 Oval(椭圆形) path
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(150, 100, 120, 80)];
// 2、设置填充色
[[UIColor brownColor] set];
[path fill];
// 3、设置画笔宽度和颜色
path.lineWidth = 2;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinBevel;
[[UIColor blueColor] set];
[path stroke];
}
最后
下一篇开始和CAShapLayer结合起来讲几个强大的实例,现在玩儿去吧。