2011-07-26 99 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);