0

簡単な描画クラスがあります。色選択バーを含むビューコントローラがあります。次に、CGRect描画機能を備えたUIView。

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

どんな助けでも大歓迎です。関連するコードスニペットは次のとおりです。

- (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;
    }
}
4

1 に答える 1

1

drawRect:は、すべてのパスを同じ色で描画しています。[currentColor setStroke]を呼び出すと、描画するすべてのストロークの色が設定されます。ストロークの色も維持し、strokeWithBlendModeを呼び出す前に色をリセットする必要があります。

何かのようなもの:

- (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にパスを追加するたびに、必ずUIColorオブジェクトをcolorArrayに追加する必要があります。

[colorArray addObject:currentColor];行の後に追加できます[pathArray addObject:myPath];

于 2012-04-15T02:21:45.223 に答える