How to draw an animatable ruler with Quartz2D?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-14 03:57:17

问题


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

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?


回答1:


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);   

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



来源:https://stackoverflow.com/questions/2532805/how-to-draw-an-animatable-ruler-with-quartz2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!