如何用Quartz2D绘制可动画制作的标尺? [英] How to draw an animatable ruler with Quartz2D?

查看:108
本文介绍了如何用Quartz2D绘制可动画制作的标尺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Quartz2D绘制一个简单的标尺,仅供参考。

I'd like to draw the lines of a simple ruler with Quartz2D, just for practice.

由于我不知道要在iPhone上以编程方式进行矢量图形绘制, ,也许有人可以给我指出一个入门的好教程?

Since I have no idea about doing vector graphics programmatically on the iPhone, maybe someone can point me to a good tutorial to get started?

推荐答案

正如Plamen指出的那样, Quartz 2D文档值得阅读。此外,课程笔记可在线获得(VoodooPad格式) iPhone开发课程,我在整个课程中都致力于Quartz 2D绘图。我创建的 QuartzExamples 示例应用程序显示了一些更高级的绘图概念,但是Apple的 QuartzDemo 示例是一个更好的起点

As Plamen points out, the Quartz 2D documentation is worth reading. Additionally, the course notes are available online (VoodooPad format) for my iPhone development course, where I devote an entire class to Quartz 2D drawing. The QuartzExamples sample application I created shows some more advanced drawing concepts, but Apple's QuartzDemo sample is a better place to start to see how you can do simple drawing.

作为标尺的绘制刻度的示例,以下是我曾经做过类似操作的代码:

As an example of drawing ticks for a ruler, the following is code that I have used to do something similar:

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。

where currentHeight is the height of the area to cover, and [MyView blackColor] simply returns a CGColorRef representing the color black.

这篇关于如何用Quartz2D绘制可动画制作的标尺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆