2011-08-02 23 views
0

iPadで指で点線を描くことができるアプリを作成しようとしています。私はすべての描画ルーチンを使って1行を作成しています。私の質問は、カーブを作成するときに重複しないような方法で二重線を描くにはどうすればいいですか?私は現在のポイントのxとyのオフセットを使ってパスに2行目を追加することでこれを達成しようとしています。これは、縦または横に移動する限り、正常に動作します。ある角度を描くと、二重線は一本の線になります。Quartzでダブルダッシュ線を描く方法

私は後で使用するためにそれらを保存できるように、NSValues(pathPoints)として保存されたポイントの配列を保持しています。始点はpathPointsの最初の点です。

UIGraphicsBeginImageContext(self.frame.size); 

    //This needs to be a dotted line 
    CGFloat pattern[] = {10,20}; 
    CGContextSetLineDash(UIGraphicsGetCurrentContext(), 3, pattern, 2); 


    CGMutablePathRef path = CGPathCreateMutable(); 
    CGMutablePathRef path2 = CGPathCreateMutable(); 


    CGPoint startingPoint = [[pathPoints objectAtIndex:0] CGPointValue]; 

    CGPathMoveToPoint(path, nil, startingPoint.x, startingPoint.y); 
    CGPathMoveToPoint(path2, nil, startingPoint.x + 10, startingPoint.y + 10); 

    for (int i = 1; i < [pathPoints count]; i++) 
    { 
     CGPoint linePoint = [[pathPoints objectAtIndex:i] CGPointValue]; 
     CGPathAddLineToPoint(path, nil, linePoint.x, linePoint.y); 
     CGPathAddLineToPoint(path2, nil, linePoint.x + 10, linePoint.y + 10); 

    } 

//もちろん、後でコンテキストにパスを追加してストロークします。

 CGContextAddPath(UIGraphicsGetCurrentContext(), path); 
     CGContextAddPath(UIGraphicsGetCurrentContext(), path2); 

     CGContextStrokePath(UIGraphicsGetCurrentContext()); 

     UIGraphicsEndImageContext(); 

私は非常に私はこれを取得することができます任意の助けに感謝。

答えて

2

最後にラインを結合しても問題ない場合は(下の画像を参照)、CGContextReplacePathWithStrokedPath()を使用してエフェクトを作成できます。

/** 
* CGContextRef context = <the context you're drawing to> 
* CGFloat lineWidth = <the width of each of your two lines> 
* CGFloat lineSpace = <the space between the two lines> 
**/ 

// Create your path here (same as you would for a single line) 

CGContextSetLineWidth(context, lineWidth + lineSpace); 
CGContextReplacePathWithStrokedPath(context); 
CGContextSetLineWidth(context, lineWidth); 
CGContextStrokePath(context); 

Stroked Path

+0

私は答えは簡単にダッシュラインに適応するために自分自身を貸すだろうと思ったので、ラインに私が要件のうち、左の点線を(、作ることが可能でしょうか? – cdasher

+0

はい。 'CGContextReplacePathWithStrokedPath()'の後に 'CGContextSetLineDash()'を呼び出すだけです。 – Craz