2

我想用 Quartz2D 画一个简单的尺子的线条,只是为了练习。

由于我不知道在 iPhone 上以编程方式进行矢量图形,也许有人可以指点我一个好的教程来开始?

4

1 回答 1

4

正如 Plamen 指出的,Quartz 2D 文档值得一读。此外,我的 iPhone 开发课程的课程笔记可在线获得(VoodooPad 格式),我在该课程中将整个课程用于 Quartz 2D 绘图。我创建的QuartzExamples示例应用程序展示了一些更高级的绘图概念,但 Apple 的QuartzDemo示例是开始了解如何进行简单绘图的更好地方。

作为为标尺绘制刻度的示例,以下是我用来执行类似操作的代码:

NSInteger minorTickCounter = majorTickInterval;
NSInteger totalNumberOfTicks = totalTravelRangeInMicrons / minorTickSpacingInMicrons;
CGFloat minorTickSpacingInPixels = currentHeight / (CGFloat)totalNumberOfTicks;

CGContextSetStrokeColorWithColor(context, [MyView blackColor]);

for (NSInteger currentTickNumber = 0; currentTickNumber < totalNumberOfTicks; currentTickNumber++)
{
    CGContextMoveToPoint(context, leftEdgeForTicks + 0.5, round(currentTickNumber * minorTickSpacingInPixels) + 0.5);

    minorTickCounter++;
    if (minorTickCounter >= majorTickInterval)
    {
        CGContextAddLineToPoint(context, round(leftEdgeForTicks + majorTickLength) + 0.5, round(currentTickNumber * minorTickSpacingInPixels) + 0.5);
        minorTickCounter = 0;               
    }
    else
    {
        CGContextAddLineToPoint(context, round(leftEdgeForTicks + minorTickLength) + 0.5, round(currentTickNumber * minorTickSpacingInPixels) + 0.5);
    }
}

CGContextStrokePath(context);   

其中currentHeight是要覆盖的区域的高度,并[MyView blackColor]简单地返回一个表示黑色的 CGColorRef。

于 2010-03-29T16:02:22.127 回答