#import "BezierPathView.h"
#define kDegreesToRadians(degrees) ((pi * degrees) / 180)
@implementation BezierPathView
- (void)drawRect:(CGRect)rect {
// 画三角形
[self drawTrianglePath];
// 画矩形
[self drawRectPath];
// 画圆
[self drawCiclePath];
// 画带圆角的矩形
[self drawRoundedRectPath];
// 画弧
[self drawARCPath];
// 画二次贝塞尔曲线
[self drawSecondBezierPath];
// 画三次贝塞尔曲线
[self drawThirdBezierPath];
}
- (void)drawTrianglePath
{
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(20, 20)];
[path addLineToPoint:CGPointMake(60, 20)];
[path addLineToPoint:CGPointMake(40, 40)];
[path closePath];
// 设置线宽
path.lineWidth = 1.5;
// 设置填充颜色
UIColor *fillColor = [UIColor greenColor];
[fillColor set];
[path fill];
// 设置画笔颜色
UIColor *strokeColor = [UIColor blueColor];
[strokeColor set];
// 根据我们设置的各个点连线
[path stroke];
}
- (void)drawRectPath
{
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(100, 20, 100, 50)];
path.lineWidth = 1.5;
/*
kCGLineCapButt, // 默认
kCGLineCapRound, // 轻微圆角
kCGLineCapSquare // 正方形
*/
path.lineCapStyle = kCGLineCapRound;
/*
kCGLineJoinMiter, // 默认表示斜接
kCGLineJoinRound, // 圆滑衔接
kCGLineJoinBevel // 斜角连接
*/
path.lineJoinStyle = kCGLineJoinBevel;
// 设置填充颜色
UIColor *fillColor = [UIColor greenColor];
[fillColor set];
[path fill];
// 设置画笔颜色
UIColor *strokeColor = [UIColor blueColor];
[strokeColor set];
// 根据我们设置的各个点连接
[path stroke];
}
- (void)drawCiclePath
{
// 传的是正方形就可以画圆了(长方形--椭圆)
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(230, 20, 100, 100)];
// 设置填充颜色
UIColor *fillColor = [UIColor greenColor];
[fillColor set];
[path fill];
// 设置画笔颜色
UIColor *strokeColor = [UIColor blueColor];
[strokeColor set];
// 根据我们设置的各个点连接
[path stroke];
}
- (void)drawRoundedRectPath
{
// 矩形的所有角都变成圆角
// UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 130, 100, 50) cornerRadius:10];
// 指定矩形的某个角为圆角
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 130, 100, 50) byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(15, 15)];
// 设置填充颜色
UIColor *fillColor = [UIColor greenColor];
[fillColor set];
[path fill];
// 设置画笔颜色
UIColor *strokeColor = [UIColor blueColor];
[strokeColor set];
// 根据我们设置的各个点连线
[path stroke];
}
// 画弧
- (void)drawARCPath
{
const CGFloat pi = 3.14159265359;
CGPoint center = CGPointMake(230, 230);
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:100 startAngle:0 endAngle:kDegreesToRadians(145) clockwise:YES];
// 有什么区别?
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound;
path.lineWidth = 5.0;
UIColor *strokeColor = [UIColor redColor];
[strokeColor set];
[path stroke];
}
// 画二次贝塞尔曲线
- (void)drawSecondBezierPath
{
UIBezierPath *path = [UIBezierPath bezierPath];
// 首先设置一个起点
[path moveToPoint:CGPointMake(20, 230)];
// 添加二次曲线(设置终端点和控制点)
[path addQuadCurveToPoint:CGPointMake(120, 350) controlPoint:CGPointMake(self.frame.size.width / 2, 120)];
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound;
path.lineWidth = 5.0;
UIColor *strokeColor = [UIColor redColor];
[strokeColor set];
[path stroke];
}
// 画三次贝塞尔曲线
- (void)drawThirdBezierPath
{
UIBezierPath *path = [UIBezierPath bezierPath];
// 设置起始端点
[path moveToPoint:CGPointMake(20, 150)];
// 设置终止端点和控制点1、控制点2
[path addCurveToPoint:CGPointMake(300, 150) controlPoint1:CGPointMake(160, 0) controlPoint2:CGPointMake(160, 250)];
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound;
path.lineWidth = 5.0;
UIColor *strokeColor = [UIColor redColor];
[strokeColor set];
[path stroke];
}
@end