2011-07-26 54 views
1

我正在iPhone上构建一个应用程序,我需要绘制一个7位段显示,它将显示用户输入的数字。我使用CGPath和CGcontext 。 我已经在数字时钟中画出了7位段。但是,我没有得到显示给定的用户的方式。要绘制一个7位段显示器,我已经完成了..以数字形式显示数字(数字时钟)

CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGLineCap cap =kCGLineCapRound; 
    center = CGPointMake(120, 130.0); 
    CGContextSetRGBStrokeColor(context, 0.5, 0.0, 1.0, 0.5); 
    path = CGPathCreateMutable(); 
    CGContextSetLineWidth(context, 08.0); 
    CGContextSetLineCap(context, cap); 
    CGPoint aOnePoint=CGPointMake(center.x ,center.y); 
    CGPoint aTwoPoint=CGPointMake(center.x ,center.y+30); 
    CGPoint points[]= {aOnePoint,aTwoPoint};  

    CGAffineTransform translation1 = CGAffineTransformMakeTranslation(0, 30); 
    CGAffineTransform translation2 = CGAffineTransformTranslate(translation1, 0, 42); 
    CGAffineTransform translation3 = CGAffineTransformTranslate(translation1, 42,0); 
    CGAffineTransform translation4 = CGAffineTransformTranslate(translation1, 42, 42); 
    CGAffineTransform translation7 = CGAffineTransformMakeTranslation(-270, 0); 
    CGAffineTransform translation5 = CGAffineTransformMakeRotation(-M_PI/2.0); 
    CGAffineTransform translation8 = CGAffineTransformMakeTranslation(-4, +3); 
    CGAffineTransform translation6 = CGAffineTransformConcat(CGAffineTransformConcat(translation7, translation5),translation8); 
    CGAffineTransform translation9 = CGAffineTransformTranslate(translation6, -44, 0); 
    CGAffineTransform translation10 = CGAffineTransformTranslate(translation6,-86, 0); 

    CGPathAddLines(path, &translation1 ,points,2); 
    CGPathAddLines(path, &translation2,points,2); 
    CGPathAddLines(path, &translation3 ,points,2); 
    CGPathAddLines(path, &translation4 ,points,2); 
    CGPathAddLines(path, &translation6 ,points,2); 
    CGPathAddLines(path, &translation9 ,points,2); 
    CGPathAddLines(path, &translation10 ,points,2); 

    CGContextBeginPath(context); 
    CGContextAddPath(context, path); 
    CGPathRelease(path); 
    CGContextStrokePath(context); 
    CGContextSaveGState(context); 
    CGContextClosePath(context); 

任何帮助将不胜感激。

+0

你也可以使用自定义字体:http://www.dafont.com/ds-digital.font – rckoenes

+0

但是我没有使用自定义字体。 – iCoder4777

回答

3

你真的需要画吗?我相信10个图像,每个数字一个,会更加令人满意。从评论

编辑:
如果你命名你的图片0.png1.png2.png等,你可以使用由用户输入的整数值来改变显示的图像(S)非常快。如果您担心会浪费磁盘空间,那真的不是问题,因为如果压缩得好,每张图片的重量不应超过5kB。

+0

不,我需要绘制一个数字0的路径,然后基于数字输入我需要改变路径。我不需要10张图片。 – iCoder4777

+0

我认为你可以用图像获得更好的设计,如果你给图像命名为0.png 1.png 2.png等,你可以使用整数值来快速切换显示的图像。如果您担心会浪费磁盘空间,那真的不是问题,因为如果压缩得好,每张图片的重量不应超过5kB。但这取决于你;) – Axel

+0

thnx巴迪,我现在将切换到你的答案。 – iCoder4777

0

效率更高,为什么不使用标签digital font。这将为您节省大量工作,并为您提供很大的灵活性和控制力。下面是一个简单的例子

timerDisplay = Label::createWithTTF("00:00:00", "fonts/digital-7.ttf", 32); 
timerDisplay->setPosition(Point((visibleSize.width - 
timerDisplay->getContentSize().width)/2, visibleSize.height)); 
timerDisplay->setAlignment(TextHAlignment::CENTER); 
this->addChild(timerDisplay);