0

myViewというUIViewがあります

@interface MyView : UIView {    UIImage *myPic;
        NSMutableArray *myDrawing; }

@end

そして、タッチを使用してこの配列を更新しました。タッチが移動し、タッチが値を追加して終了しました。

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 //   myDrawing = [[NSMutableArray alloc] initWithCapacity:4];
   [myDrawing addObject:[[NSMutableArray alloc] initWithCapacity:4]]; 
    CGPoint curPoint = [[touches anyObject] locationInView:self]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint curPoint = [[touches anyObject] locationInView:self]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]]; 
    [self setNeedsDisplay];
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint curPoint = [[touches anyObject] locationInView:self]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];
    [self setNeedsDisplay];
}

次に、drawrectメソッドを使用して線を更新します

- (void)drawRect:(CGRect)rect
{
    // Drawing code


    float newHeight;
    float newWidth; 

    if (!myDrawing) {
        myDrawing = [[NSMutableArray alloc] initWithCapacity:0];
    }

    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    if (myPic != NULL) 
    {
        float ratio = myPic.size.height/460; 
        if (myPic.size.width/320 > ratio) 
        {
            ratio = myPic.size.width/320;
        }  

        newHeight = myPic.size.height/ratio;
        newWidth = myPic.size.width/ratio;
        [myPic drawInRect:CGRectMake(0,0,newWidth,newHeight)];
    }

    if ([myDrawing count] > 0) {
        CGContextSetLineWidth(ctx, 3);
        NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"SwatchColor"];
        UIColor *color;
        if (colorData!=nil) {
            // If the data object is valid, unarchive the color we've stored in it.
            color = (UIColor *)[NSKeyedUnarchiver unarchiveObjectWithData:colorData];

        }
        if (color)
        {
            CGContextSetStrokeColorWithColor(ctx, color.CGColor);  
        }
        else
        {
            CGContextSetStrokeColorWithColor(ctx,[UIColor blackColor].CGColor);
        }

        for (int i = 0 ; i < [myDrawing count] ; i++) {
            NSArray *thisArray = [myDrawing objectAtIndex:i];
            if ([thisArray count] > 2) 
            {
                float thisX = [[thisArray objectAtIndex:0] floatValue];
                float thisY = [[thisArray objectAtIndex:1] floatValue];



                CGContextBeginPath(ctx);
                CGContextMoveToPoint(ctx, thisX, thisY);

            for (int j = 2; j < [thisArray count] ; j+=2) 
            { 
                thisX = [[thisArray objectAtIndex:j] floatValue];
                thisY = [[thisArray objectAtIndex:j+1] floatValue];

                CGContextAddLineToPoint(ctx, thisX,thisY);
                //CGContextAddQuadCurveToPoint(ctx, 150, 10, thisX, thisY);
                 // CGContextAddCurveToPoint(ctx , 0, 50, 300, 250, thisX, thisY);
            }
                CGContextStrokePath(ctx);

            }
        }
    }

}

コードに色を変更するカラーピッカーがあり、色を選択して毎回異なる色の線を描きたいのですが、今のところ、最初に赤を選択して線を引くときに線を作成してレンダリングしているので、その後、青を選択して線を引きます。古い線も赤ではなく青に変わりますが、赤を赤のままにしておきたいので、青をそのままにしておくとよいでしょうか。

4

1 に答える 1

1

あなたはいつも描かれた線の上に描いています。クリアmyDrawingして、処理する必要のあるポイントのみを保存し、元に戻す/最適化された保存機能が必要な場合は、すでに処理されたポイントを別の配列に保持します。

于 2012-05-28T11:27:20.277 に答える