0
我有一個UIView稱爲MyView的CoreGraphics中呈現兩個或更多個行具有不同顏色的
@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];
}
然後我使用繪圖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);
}
}
}
}
我有一個顏色選擇器中我的代碼來改變顏色,我想得出的不同顏色的線,每次通過選擇顏色,但現在,因爲我正在構建線條和渲染它,當我最初選擇紅色並繪製線條,然後選擇藍色並繪製線條,現在舊線條也變成藍色而不是紅色,但是我想要紅色保持紅色,藍色可以幫助任何人?
A-Live那麼舊的線條將如何繪製?沒有提供任何分數? – raghul
我唯一擔心的是,當我以您告訴的方式移除舊線路時,我會希望舊線路保持原樣! – raghul
嗯,這是正確的:)你必須將每個點的顏色與它的座標一起存儲(如果你想保存點信息以便稍後重新繪製,那麼無論如何都需要這些顏色)。我也會嘗試保存和重用圖層,但說實話,不確定它是否可以正常工作,沒有副作用。 –