2012-01-24 2 views
1

したがって、2つの異なる線を描く必要があります。別のpostingを通して、私は行を再描画する方法を考え出しました。私の質問は、私は2行を描画したい場合、私は2つのコードセグメントをそれを行う必要がありますか?またはn行を描画する方法はありますか?複数の描画線を作成する

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    NSLog(@"drawRect called"); 
    CGContextSetLineWidth(context, self.lineWidth); 
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor); 
    CGContextMoveToPoint(context, self.startPoint.x, self.startPoint.y); 
    CGContextAddLineToPoint(context, self.endPoint.x, self.endPoint.y); 
    CGContextStrokePath(context); 
} 

実装

draw2D *myCustomView = [[draw2D alloc] init]; 

myCustomView.startPoint = CGPointMake(0, 0); 
myCustomView.endPoint = CGPointMake(300, 300); 
myCustomView.lineWidth = 5; 
myCustomView.lineColor = [UIColor redColor]; 

myCustomView.frame = CGRectMake(0, 0, 500, 500); 
[myCustomView setBackgroundColor:[UIColor blueColor]]; 
[self.view addSubview:myCustomView]; 
[myCustomView setNeedsDisplay]; 

myCustomView.endPoint = CGPointMake(100, 100); 

myCustomView.lineColor = [UIColor orangeColor]; 
[myCustomView setNeedsDisplay]; 

これはただの行を再描画します。私はそれぞれの行のdrawRectブロックを繰り返しますか?だから私は2行が必要な場合、私はする必要がありますか:

- (void)drawRect:(CGRect)rect 
{ 
    //line1 
    CGContextRef context1 = UIGraphicsGetCurrentContext(); 
    NSLog(@"drawRect called"); 
    CGContextSetLineWidth(context1, self.lineWidth1); 
    CGContextSetStrokeColorWithColor(context1, self.lineColor1.CGColor); 
    CGContextMoveToPoint(context1, self.startPoint1.x, self.startPoint1.y); 
    CGContextAddLineToPoint(context1, self.endPoint1.x, self.endPoint1.y); 
    CGContextStrokePath(context1); 

    //line 2 
    CGContextRef context2 = UIGraphicsGetCurrentContext(); 
    NSLog(@"drawRect called"); 
    CGContextSetLineWidth(context2, self.lineWidth2); 
    CGContextSetStrokeColorWithColor(context2, self.lineColor2.CGColor); 
    CGContextMoveToPoint(context2, self.startPoint2.x, self.startPoint2.y); 
    CGContextAddLineToPoint(context2, self.endPoint2.x, self.endPoint2.y); 
    CGContextStrokePath(context2); 
} 

または私の行を描画するより良い方法はありますか?

EDIT:最終的な目標は、私が形式として使用していたラベルとテキストボックスとUIViewでそれを使用することです。私はラインがフォームを分割したい。

私はちょうどこれらのカスタムを使用する必要がある場合、私は思ったんだけどだけラインとしてUIView秒draw2DとUIViewフォームの上に置き、その後、私はちょうど、複数の「draw2D」UIViewsを作成することができますか?

解決ので、ここで

は、私はそれが働くだろう考え出したコードは次のとおりです。

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 

     NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 

     NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init]; 
     [tempDict setValue:[NSValue valueWithCGPoint:CGPointMake(10, 10)] forKey:@"start"]; 
     [tempDict setValue:[NSValue valueWithCGPoint:CGPointMake(500, 500)] forKey:@"end"]; 

     [tempArray addObject:tempDict]; 

     NSMutableDictionary *tempDict2 = [[NSMutableDictionary alloc] init]; 
     [tempDict2 setValue:[NSValue valueWithCGPoint:CGPointMake(400, 10)] forKey:@"start"]; 
     [tempDict2 setValue:[NSValue valueWithCGPoint:CGPointMake(500, 500)] forKey:@"end"]; 

     [tempArray addObject:tempDict2]; 

     self.pointArray = [NSArray arrayWithArray:tempArray]; 
    } 
    return self; 
} 


// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetLineWidth(context, self.lineWidth); 
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor); 

    for (int i = 0; i < [self.pointArray count]; i++) 
    { 
     CGPoint tempPoint = [[[self.pointArray objectAtIndex:i] objectForKey:@"start"] CGPointValue]; 
     CGPoint tempPoint2 = [[[self.pointArray objectAtIndex:i] objectForKey:@"end"] CGPointValue]; 

     CGContextMoveToPoint(context, tempPoint.x, tempPoint.y); 
     CGContextAddLineToPoint(context, tempPoint2.x, tempPoint2.y); 
    } 

    CGContextStrokePath(context); 
} 

私は私のinitWithFrameが含まれていました。私はテストのために使用される配列を作成する方法の方法(するCGPointのがオブジェクトではありません、それらを辞書に含める方法を理解しなければならなかった)。

ので、これがループを通ってn行を作成します。

+0

FYIでは、 "for(NSDictionary * dict in self.pointArray){..."を使用してループを改善することができます。次に、iで配列内のオブジェクトを毎回ルックアップする必要はありません。この種のループは「高速反復」として知られており、配列や辞書内のあらゆる種類のオブジェクトで実行できます。 –

+0

興味深いことに、私は前に(idキーのNSDictionaryで)使用しましたが、これを見たことがない、私はそれを調べます。ありがとう。 – Padin215

答えて

5

あなたは(明らかに毎回異なる座標で)必要があるとして、あなたは何回もこのビットを繰り返すことができます。

第1行のコードは行の開始点を設定し、第2行はコードを描画します。残りのコードをすべて繰り返す必要はありません。

言い換えれば、あなたはこのように複数の線を描くことができます:

//set up context 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetLineWidth(context, self.lineWidth1); 
CGContextSetStrokeColorWithColor(context, self.lineColor1.CGColor); 

//line1 
CGContextMoveToPoint(context, self.startPoint1.x, self.startPoint1.y); 
CGContextAddLineToPoint(context, self.endPoint1.x, self.endPoint1.y); 

//line 2 
CGContextMoveToPoint(context, self.startPoint2.x, self.startPoint2.y); 
CGContextAddLineToPoint(context, self.endPoint2.x, self.endPoint2.y); 

//line 3 
etc... 

//finished drawing 
CGContextStrokePath(context); 

あなたは線を引く全体の多くを毎回コピーする必要はありません、あなただけのコードのこれら2つの行を繰り返すことができます。

+0

これを拡張できますか?私はあなたが言うことに従っているようには思わない。 – Padin215

+0

kだから、私はこのアイデアを使用し、私のdrawRectメソッドにループを置くだけです。私は開始点と終了点を持つインスタンス配列を設定し、必要なだけ多くの行を作成するためにそれをループします。 – Padin215

+0

私は答えを明確にして更新しました。 –

0

さて、あなたがより良いと普遍的な方法がある別の方法に重複したコードをリファクタリングして、線を引く必要がある変数に渡し、コンテキストすなわち、色、開始点と終了点、幅

+0

しかし、これは新しい行を描画するのではなく、行を再描画するだけですか? – Padin215

+0

何を描きたいですか?グリッド? @skippyが提案したものをdrawRectの中のループに入れてください。各行にiVarsを使用しないでください。ちょうど出発点のセットを持っているし、ループ中に - 値を変更する - y座標に20ポイントを追加して次のラインを20に移動するなど... – bandejapaisa

0

ができ:

- (void)drawRect:(CGRect)rect:(long)lineWidth:(CGColor)color:(CGPoint)pStart:(CGPoint)pEnd 
{ 
    //line1 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    NSLog(@"drawRect called"); 
    CGContextSetLineWidth(context, lineWidth); 
    CGContextSetStrokeColorWithColor(context, color); 
    CGContextMoveToPoint(context, pStart.x, pStart.y); 
    CGContextAddLineToPoint(context, pEnd.x, pEnd.y); 
    CGContextStrokePath(context); 
} 

...

[自己のdrawRect:RECT:self.lineWidth1:self.lineColor1.CGColor:self.startPoint1:self.endPoint1];

[自己のdrawRect:RECT:self.lineWidth2:self.lineColor2.CGColor:self.startPoint2:self.endPoint2]。

...

+0

これはラインを再描画せずに新しいライン? – Padin215

+0

ああ、私はdrawRectメソッドをオーバーロードするとは思わなかったと思います。私はそのアプローチがインスタンス変数より良いのが好きです、ありがとう。 – Padin215

+0

私はdrawRectを直接呼び出すべきではないと思いましたか?または私はここに何かを逃していますか? – Padin215