【K线】UIBezierPath-示例

  • UIBezierPath的使用方法:
    (1)创建一个Bezier path对象。
    (2)使用方法moveToPoint:去设置初始线段的起点。
    (3)添加line或者curve去定义一个或者多个subpaths。
    (4)改变UIBezierPath对象跟绘图相关的属性。

我们可以设置stroked path的属性lineWidth和lineJoinStyle。也可以设置filled path的属性usesEvenOddFillRule

多边形

- (void)drawRect:(CGRect)rect {
    UIColor *color = [UIColor redColor];
    [color set];
    // 创建UIBezierPath
    // 多边形
    UIBezierPath *apath =({
        UIBezierPath *path = [UIBezierPath bezierPath];
        path.lineWidth = 5.0f; // 设置线条宽度
        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;
    });
    [apath stroke];//空心
}

矩形

- (void)drawRect:(CGRect)rect {
    UIColor *color = [UIColor redColor];
    [color set];
    // 创建UIBezierPath
    UIBezierPath *apath = [UIBezierPath bezierPathWithRect:CGRectMake(10, 40, 200, 100)];
    //更具坐标连线
    [apath fill];//实心
}

圆形

- (void)drawRect:(CGRect)rect {
    UIColor *color = [UIColor redColor];
    [color set];
    UIBezierPath *apath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 40, 200, 100)];
    apath.lineWidth     = 5.0f;
    apath.lineCapStyle  = kCGLineCapRound;
    apath.lineJoinStyle = kCGLineCapRound;
    [apath stroke];
}

圆角矩形

- (void)drawRect:(CGRect)rect {
    UIColor *color = [UIColor redColor];
    [color set];
    UIBezierPath *apath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 40, 200, 100)
                                                byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
                                                      cornerRadii:CGSizeMake(100, 100)];
    apath.lineWidth     = 5.0f;
    apath.lineCapStyle  = kCGLineCapRound;
    apath.lineJoinStyle = kCGLineCapRound;
    [apath stroke];
}

二次贝塞尔曲线

- (void)drawRect:(CGRect)rect {
    UIColor *color = [UIColor redColor];
    [color set];
    // 创建UIBezierPath
    UIBezierPath *apath = ({
        UIBezierPath *path = [UIBezierPath bezierPath];
        path.lineWidth     = 2.0f;              //设置线条宽度
//        path.lineCapStyle = kCGLineCapRound;    //设置拐角
        //绘制二次贝赛尔曲线
        //设置起始点
        [path moveToPoint:CGPointMake(100, 300)];
        //设置EndPoint & Control Point
        [path addCurveToPoint:CGPointMake(300, 200)
                controlPoint1:CGPointMake(200, 80)
                controlPoint2:CGPointMake(220, 500)];
        path;
    });
    [apath stroke];
}

你可能感兴趣的:(【K线】UIBezierPath-示例)