开发者

how to draw continuous lines by QuartzCore in iOS

开发者 https://www.devze.com 2023-03-16 05:55 出处:网络
I have got a question about quartz core on iOS When I use Quartz to draw lines, the following happens and I can\'t find the reason:

I have got a question about quartz core on iOS

When I use Quartz to draw lines, the following happens and I can't find the reason:


when I draw the second line the first line would disappear

when I draw the third line the second would the disappear, and the 1st and 3rd would show ..

when I draw the 2n+1th line, 1,3,5,...2n-1th lines shows, and 2,4,6,8... 2n dissappears


see the code below. I don't save contexts and paths as my understandin开发者_开发问答g, I think it should be one of the two cases

  1. display all lines I drawn
  2. display the last line I drawn and the previous lines should disappear

But the two cases don't happen.

- (void)drawInContext:(CGContextRef)context {

    //  橡皮擦
    //CGContextClearRect(context, CGRectMake(0, 0, 320, 480));

    CGContextSetLineWidth(context, 4.0);
    CGContextMoveToPoint(context, previousPoint.x, previousPoint.y);
    CGContextSetRGBStrokeColor(context, 0, 1.0, 1.0, 1.0);  
    CGContextAddLineToPoint(context, nextPoint.x,nextPoint.y);
    CGContextStrokePath(context);
    //NIF_TRACE(@"began : %@ moved : %@", NSStringFromCGPoint(previousPoint),NSStringFromCGPoint(nextPoint));

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];

    previousPoint = [touch locationInView:self];
    nextPoint = [touch locationInView:self];

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];

    previousPoint = nextPoint;
    nextPoint = [touch locationInView:self];        
    [self setNeedsDisplay];

}

I saw a demo that use NSMutableArray to save all UIBezierPaths it drawn, when the View redraw, it transfer paths saved in array, restores them in drawRect:

UIBezierPath is a wrapper of Objective C, and it only works in 3.2+ I need do something make it work in 3.0+ I think it must be exists a better method to save contexts and paths (Colors,Paths,strokeWidths)

Anybody have ideas?


Every time you "drawInContext" you are clearing the drawing area and placing the new line.... This clear code:

CGContextClearRect(context, CGRectMake(0, 0, 320, 480));

Needs to be somewhere else in your code... somewhere where it will only be activated 1 time (or if you need to clear the whole drawing (perhaps an erase all function))


You need to save the image on canvas:

CGImageRef  imageRef;

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();

    if (imageRef) {
        // Restore the screen that was previously saved
        CGContextTranslateCTM(context, 0, rect.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);

        CGContextDrawImage(context, rect, imageRef);
        CGImageRelease(imageRef);

        CGContextTranslateCTM(context, 0, rect.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
    }

    // Your drawing code here...

    imageRef = CGBitmapContextCreateImage(context);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号