0%

贝塞尔曲线的基本用法

画五边形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 画五边形
- (void)drawPentagons {
UIBezierPath *apath = ({
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineJoinStyle = kCGLineCapRound;
path.lineCapStyle = kCGLineCapRound;
path.lineWidth = 5.0;

//设置起始点
[path moveToPoint:CGPointMake(100, 0)];
//增加线条
[path addLineToPoint:CGPointMake(200, 40)];
[path addLineToPoint:CGPointMake(160, 140)];
[path addLineToPoint:CGPointMake(40, 140)];
[path addLineToPoint:CGPointMake(0, 40)];

//关闭路径
[path closePath];

path;
});
//根据坐标连线
[apath stroke];
}

画矩形

1
2
3
4
5
6
7
8
// 画矩形
- (void)drawRectangle {
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 40, 200, 100)];
path.lineWidth = 5.0;
// path.lineCapStyle = kCGLineCapRound;
// path.lineJoinStyle = kCGLineCapSquare;
[path stroke];
}

画圆和椭圆

1
2
3
4
5
6
// 画圆和椭圆
- (void)drawCircle {
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 40, 200, 200)];
path.lineWidth = 5.0;
[path stroke];
}

画带指定圆角的矩形

1
2
3
4
5
6
7
8
// 画带指定圆角的矩形
- (void)drawOtherRectangle {
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 40, 200, 100) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(100, 100)];
path.lineWidth = 5.0;
path.lineJoinStyle = kCGLineCapRound;
path.lineCapStyle = kCGLineCapRound;
[path stroke];
}

画圆弧

1
2
3
4
5
6
7
8
// 画圆弧
- (void)drawCircleArc {
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 200) radius:100 startAngle:M_PI endAngle:M_PI / 2 clockwise: NO];
path.lineWidth = 5.0;
path.lineJoinStyle = kCGLineCapRound;
path.lineCapStyle = kCGLineCapRound;
[path stroke];
}

画贝塞尔曲线


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 画贝塞尔曲线
- (void)drawBezierArc {
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 2.0;
path.lineJoinStyle = kCGLineCapRound;
path.lineCapStyle = kCGLineCapRound;

// 曲线1
/*
// 设置起始点
[path moveToPoint:CGPointMake(100, 300)];
// 设置结束点和控制点
[path addQuadCurveToPoint:CGPointMake(300, 300) controlPoint:CGPointMake(100, 50)];
*/

// 曲线2
// [path moveToPoint:CGPointMake(10, 100)];
// [path addQuadCurveToPoint:CGPointMake(310, 100) controlPoint:CGPointMake(150, -90)];

// 曲线3 三次贝塞尔曲线
[path moveToPoint:CGPointMake(10, 100)];
[path addCurveToPoint:CGPointMake(310, 100) controlPoint1:CGPointMake(100, -100) controlPoint2:CGPointMake(100, 400)];
[path stroke];
}