【问题标题】:CoreGraphics rendering two or more lines with different colorsCoreGraphics 用不同颜色渲染两条或多条线
【发布时间】:2012-05-28 11:07:27
【问题描述】:

我有一个名为 myView 的 UIView

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

@end

我使用 touches 开始更新这个数组,在 touches 移动和 touches 结束时添加值。

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

然后我使用 draw rect 方法来更新线条

- (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);

            }
        }
    }

}

我的代码中有一个颜色选择器来更改颜色,我想每次通过选择颜色来绘制不同颜色的线条,但到目前为止,因为我正在构建线条并在我最初选择红色时渲染它并画一条线,然后选择蓝色并画一条线,现在旧线也变为蓝色而不是红色,但是我希望红色保持红色,蓝色这样的任何人都可以帮忙吗?

【问题讨论】:

    标签: iphone objective-c ios core-graphics


    【解决方案1】:

    你总是在画线上画画。清除 myDrawing 使其仅存储需要处理的点,如果您需要撤消/优化保存功能,请将已处理的点保留在另一个数组中。

    【讨论】:

    • A-Live 那么旧线将如何绘制?不给分?
    • 我唯一担心的是,当我按照您所说的方式删除时,旧线会消失,我希望旧线保持原样!!
    • 嗯,没错 :) 您必须将每个点的颜色与其坐标一起存储(如果您想保存点信息以便以后重新绘制它,无论如何都需要这样做)。我也尝试保存和重用该层,但老实说,不确定它是否可以在没有副作用的情况下工作。
    猜你喜欢
    • 2021-11-14
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    相关资源
    最近更新 更多