贝塞尔曲线UIBezierPath

UIBezierPath类继承NSObject,在UIKit框架,是Core Graphics框架关于path的一个封装,可以定义简单的形状;Path如果是基于矢量形状的,都用直线和曲线段去创建。每一段包括一个或多个点,每一个path的结束的地方就是下一个path的起点。

使用方法(self一定要继承UIView)

- (void)drawRect:(CGRect)rect {
    // 1.初始化一个UIBezierPath对象
    UIBezierPath *path = [UIBezierPath bezierPath];
    // 2.设备初始线段的起点位置(点的位置是相对于原点(0, 0))
    [path moveToPoint:CGPointMake(<#CGFloat x#>, <#CGFloat y#>)];
    // 3.添加线段
    [path addLineToPoint:CGPointMake(<#CGFloat x#>, <#CGFloat y#>)];  // 直线
    [path addCurveToPoint:<#(CGPoint)#> controlPoint1:<#(CGPoint)#> controlPoint2:<#(CGPoint)#>];  // 曲线
    // 4.设置属性
}

1.画多边形(思路:设置点然后,点与点连线)

- (void)drawRect:(CGRect)rect {

    // 设置颜色
    UIColor *blueColor = [UIColor blueColor];
    [blueColor set];

    // 初始化
    UIBezierPath *path = [UIBezierPath bezierPath];
    // 线宽
    path.lineWidth = 5;
    path.lineCapStyle = kCGLineCapRound;  // 线条拐角
    path.lineJoinStyle = kCGLineCapRound;   // 终点处理
    // 起点
    [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 stroke]; // 根据坐标连线stroke线条着色,fill内部着色
}

2.矩形

- (void)drawRect:(CGRect)rect {
    // 设置颜色
    UIColor *grayColor = [UIColor grayColor];
    [grayColor set];
    CGRect pathRect = CGRectMake(20, 20, 80, 80);
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:pathRect];
    path.lineWidth = 1;
    path.lineCapStyle = kCGLineCapSquare;  // 线条拐角
    path.lineJoinStyle = kCGLineCapSquare; // 终点处理
    [path stroke];
}

3.圆或椭圆

[UIBezierPath bezierPathWithOvalInRect:<#(CGRect)#>];根据传入的rect矩形参数绘制一个内切曲线,rect是正方形就是圆形,rect是长方形就是椭圆

- (void)drawRect:(CGRect)rect {
    // 设置颜色
    UIColor *grayColor = [UIColor grayColor];
    [grayColor set];

    // 圆
    CGRect rect = CGRectMake(10, 10, 80, 80);     // 正方形
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
    path.lineWidth = 1;
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineCapSquare;
    [path stroke];

    // 椭圆
    CGRect ovalRect = CGRectMake(100, 10, 80, 60); // 长方形
    UIBezierPath *ovalPath = [UIBezierPath bezierPathWithOvalInRect:ovalRect];
    ovalPath.lineWidth = 1;
    ovalPath.lineCapStyle = kCGLineCapRound;
    ovalPath.lineJoinStyle = kCGLineCapRound;
    [ovalPath fill];
}

4.弧线

以X轴正方向开始计算

- (void)drawRect:(CGRect)rect {
    UIColor *grayColor = [UIColor grayColor];
    [grayColor set];

    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(20, 20) radius:40 startAngle:0 endAngle:M_PI/2 clockwise:YES];

    path.lineWidth = 1;
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineCapRound;
    [path fill];
}

[UIBezierPath bezierPathWithArcCenter:<#(CGPoint)#>                 // 圆弧中心
radius:<#(CGFloat)#>        // 半径
startAngle:<#(CGFloat)#>    // 开始角度 
endAngle:<#(CGFloat)#>      // 结束角度
clockwise:<#(BOOL)#>]       // 是否顺时针

效果如下:

results matching ""

    No results matching ""