2011-06-29 40 views
0

畫實線我有一個關於iOS上如何通過QuartzCore iOS中

石英核心問題當我使用Quartz繪製線條,將出現以下情況,我無法找到原因:


當我繪製的第二行的第一行就會消失

當我繪製第三行第二會消失不見,而第一和第三將顯示 ..

當我畫了2N + 1條線,1,3,5,... 2N-1號線節目,2,4 ,6,8 ... 2N dissappears


看到下面的代碼。我沒有背景和路徑 保存爲我的理解,我覺得應該是這兩種情況一個

  1. 顯示我畫的所有行
  2. 顯示我最後繪製的線條和前行應該消失

但是這兩種情況都沒有發生。

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

} 

我看到使用的NSMutableArray保存它繪製所有UIBezierPaths演示,當查看重繪,它傳送保存在陣列的路徑,恢復他們的drawRect:

UIBezierPath是目標C的包裝,並且它只能在3.2+ 我需要做一些事情讓它在3.0+ 工作,我想它一定是存在一個更好的方法來保存環境和路徑(顏色,路徑,strokeWidths)

任何人有想法?

回答

1

每次你 「drawInContext」 你正在清理繪圖區域,並把新的生產線....這清楚代碼:

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

需要在某處否則在你的代碼中......某處它只會被激活1次(或者如果你需要清除整個繪圖(可能是全部擦除功能))

+0

NO **它的註釋**'// CGContextClearRect(背景下,CGRectMake(0,0,320,480));'這個問題是不存在的。 – aelam

0

您需要保存在畫布上的圖像:

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