如何使用UIBezierPath绘制一条弧线

时间:2014-03-08 10:21:55

标签: ios core-graphics uibezierpath

我正在尝试画出如图所示的形状。背景是白色的。希望你能看到它..

enter image description here

我正在使用bezier路径来绘制它。我提供了如蓝色边框所示的边界形状。

enter image description here

到目前为止,我成功地只绘制了两条线(以绿色显示)。我必须进一步用红色画一个。

enter image description here

我无法从这一点画出弧线。我无法理解如何将正确的参数传递给addArcWithCenter

代码

-(void) drawRect:(CGRect)rect
{
    //declare and instantiate the UIBezierPath object
    aPath = [UIBezierPath bezierPath];

    // Set the starting point of the shape.
    [aPath moveToPoint:CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect))];

    // Draw some lines.
    [aPath addLineToPoint:CGPointMake(CGRectGetMaxX(rect), CGRectGetMinY(rect))];
    [aPath addLineToPoint:CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect) - 40)];
    [aPath addArcWithCenter:self.center radius:40 startAngle:3 *(M_PI / 2) endAngle:M_PI clockwise:NO];

    //set the line width
    aPath.lineWidth = 2;

    //set the stoke color
    [[UIColor greenColor] setStroke];

    //draw the path
    [aPath stroke];

}

我是核心图形新手。请对我宽容..谢谢..

1 个答案:

答案 0 :(得分:5)

试试这个(正如你所看到的,我已经使用了addQuadCurveToPoint @Wain提出的addCurveToPoint变体 - 请google for addCurveToPoint并切换到图片搜索以查看它是如何工作的):

-(void) drawRect:(CGRect)rect
{
    UIBezierPath * aPath = [UIBezierPath bezierPath];

    // Set the starting point of the shape.
    [aPath moveToPoint:CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect))];

    // Draw some lines.
    [aPath addLineToPoint:CGPointMake(CGRectGetMaxX(rect), CGRectGetMinY(rect))];



//changes start here !

    //the point look to be at 80% down
    [aPath addLineToPoint:CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect) * .8)];


    //1st arc
    //The end point look to be at 1/4 at left, bottom
    CGPoint p = CGPointMake(CGRectGetMaxX(rect) / 4, CGRectGetMaxY(rect));
    CGPoint cp = CGPointMake( (CGRectGetMaxX(rect) / 4) + ((CGRectGetMaxX(rect) - (CGRectGetMaxX(rect) / 4)) / 2) , CGRectGetMaxY(rect) * .8);

    [aPath addQuadCurveToPoint:p controlPoint:cp];


    //2nd arc
    //The end point look to be at 80% downt at left,
    CGPoint p2 = CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect) * .8);
    CGPoint cp2 = CGPointMake( (CGRectGetMaxX(rect) / 4) / 2 , CGRectGetMaxY(rect) * .8);

    [aPath addQuadCurveToPoint:p2 controlPoint:cp2];


    //close the path
    [aPath closePath];

    //set the line width
    aPath.lineWidth = 2;

    //set the stoke color
    [[UIColor greenColor] setStroke];

    //draw the path
    [aPath stroke];
}