我如何使用CGContext绘制它

时间:2010-03-02 00:54:20

标签: iphone cgcontext

我想在我的视图中绘制这一点来绘制这条线,我拥有制作基本线所需的一切,但我只是不擅长绘画,我确实尝试过这样做,但是无法得到它工作正常。 alt text http://www.freeimagehosting.net/uploads/50b06b1701.jpg

2 个答案:

答案 0 :(得分:2)

这是Bézier curve吗?如果您知道两个控制点的位置,请使用

CGContextMoveToPoint(context, x, y);
CGContextAddCurveToPoint(context, ...); // Cubic Bézier curve

CGContextMoveToPoint(context, x, y);
CGContextAddQuadCurveToPoint(context, ...); // Quadratic Bézier curve

插入曲线,然后使用

CGContextStrokePath(context);

划线。

答案 1 :(得分:2)

以下代码应该绘制一条正如你所描述的那样的正弦曲线,假设currentBounds是你所在区域内绘制的边界矩形:

CGContextBeginPath(context);
CGContextMoveToPoint(context, 0.0f, CGRectGetMidY(currentBounds));
CGContextAddCurveToPoint(context, currentBounds.size.width / 5.0f, CGRectGetMidY(currentBounds) - currentBounds.size.width / 5.0f, CGRectGetMidX(currentBounds) - currentBounds.size.width / 5.0f, CGRectGetMidY(currentBounds) - currentBounds.size.width / 5.0f, CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds));
CGContextAddCurveToPoint(context, CGRectGetMidX(currentBounds) + currentBounds.size.width / 5.0f, CGRectGetMidY(currentBounds) + currentBounds.size.width / 5.0f, currentBounds.size.width - currentBounds.size.width / 5.0f, CGRectGetMidY(currentBounds) + currentBounds.size.width / 5.0f, currentBounds.size.width, CGRectGetMidY(currentBounds));
CGContextClosePath(context);
CGContextStrokePath(context);