2012-04-15 9 views
0

私は単純な描画クラスを持っています。色選択バーを含むビューコントローラがあります。次に、CGRect描画関数を持つUIView。xCode draw - CGRectで色の選択を維持しようとしています

色を変更すると、既存のすべてのストロークが変更されます。私は何を台無しにしたのですか?新しいストロークの色を変更したいだけです。

何か歓迎致します。関連するコードスニペットは次のとおりです。

- (void)drawRect:(CGRect)rect 
{ 
    [currentColor setStroke]; 

     for (UIBezierPath *_path in pathArray) 
    [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; 
} 

#pragma mark - Touch Methods 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    swiped = NO; 

    myPath=[[UIBezierPath alloc]init]; 
    myPath.lineWidth=10; 
    UITouch *touch= [touches anyObject]; 
    [myPath moveToPoint:[touch locationInView:self]]; 
    [pathArray addObject:myPath]; 

    if ([touch tapCount] == 2) { 
     [self eraseButtonClicked]; 
     return; 
    } 

    lastPoint = [touch locationInView:self]; 
    lastPoint.y -= 20; 


} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    swiped = YES; 

    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self]; 
    currentPoint.y -= 20; 

    [myPath addLineToPoint:[touch locationInView:self]]; 
    [self setNeedsDisplay]; 

    UIGraphicsBeginImageContext(self.frame.size); 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 15.0); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 

    moved++;  
    if (moved == 10) { 
     moved = 0; 
    } 

} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    if ([touch tapCount] == 2) { 
     [self eraseButtonClicked]; 
     return; 
    } 
} 

答えて

1

drawRect:すべてのパスを同じ色で描画しています。 [currentColor setStroke]を呼び出すと、描画するすべての線の色が設定されます。ストロークの色を維持し、strokeWithBlendModeを呼び出す前に色をリセットする必要があります。あなたはcolorArrayにあなたがpathArrayへのパスを追加するたびにUIColorオブジェクトを追加を確認する必要があります

- (void)drawRect:(CGRect)rect 
{ 
    for(int i=0; i<[pathArray count]; i++) { 
     [[colorArray objectAtIndex:i] setStroke]; 
     [[pathArray objectAtIndex:i] strokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; 
    } 
} 

:よう

何か。

あなたは、ライン[pathArray addObject:myPath];

+0

こんにちはタイラー後に答えるためのおかげで[colorArray addObject:currentColor];を追加することができます。おそらく私は完全に理解していない..私は7色のセグメント化されたコントロールを持っています。あなたはそれらの配列を作るべきだと言っていますか?ユーザーが特定のボタンをタップするとどうなりますか。詳しく教えてください。回答をいただきありがとうございます。David –

+0

ユーザーがカラーコントロールをタップしたときにcurrentColor変数を変更しているとします。これでうまくいくが、新しいパスをpathArrayに追加するときにユーザーが選択した色を追跡する必要がある。 PathArrayと一致する新しい配列にcurrentColorを追加することをお勧めします。 –

+0

また、pathArrayからパスを削除すると、対応する色もcolorArrayから削除する必要があります。そうしないと、対応する色は同期されません。 –

関連する問題