Iphone 绘制形状时,线将被擦除
我试图通过触摸屏幕来制作一个在屏幕上绘制形状的应用程序 我可以从一个点到另一个点画一条线,但每次新画的时候它都会被抹去 这是我的密码:Iphone 绘制形状时,线将被擦除,iphone,path,draw,shapes,cg,Iphone,Path,Draw,Shapes,Cg,我试图通过触摸屏幕来制作一个在屏幕上绘制形状的应用程序 我可以从一个点到另一个点画一条线,但每次新画的时候它都会被抹去 这是我的密码: CGPoint location; CGContextRef context; CGPoint drawAtPoint; CGPoint lastPoint; -(void)awakeFromNib{ //[self addSubview:noteView]; } -(void)touchesMoved:(NSSet *)touches withEve
CGPoint location;
CGContextRef context;
CGPoint drawAtPoint;
CGPoint lastPoint;
-(void)awakeFromNib{
//[self addSubview:noteView];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
location = [touch locationInView:touch.view];
[self setNeedsDisplayInRect:CGRectMake(0, 0, 320, 480)];
}
- (void)drawRect:(CGRect)rect {
context = UIGraphicsGetCurrentContext();
[[UIColor blueColor] set];
CGContextSetLineWidth(context,10);
drawAtPoint.x =location.x;
drawAtPoint.y =location.y;
CGContextAddEllipseInRect(context,CGRectMake(drawAtPoint.x, drawAtPoint.y, 2, 2));
CGContextAddLineToPoint(context,lastPoint.x, lastPoint.y);
CGContextStrokePath(context);
lastPoint.x =location.x;
lastPoint.y =location.y;
}
谢谢你的帮助-
Nir。正如您所发现的,-drawRect是显示视图内容的地方。你只能在屏幕上“看到”你在这里画的东西 这比Flash更低级,在Flash中,你可以在舞台上添加一个包含一行的movieclip,稍后再在舞台上添加另一个包含一行的movieclip,现在你可以看到-两行 你将需要做一些工作,并可能设置一些类似于
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
location = [touch locationInView:touch.view];
[self addNewLineFrom:lastPoint to:location];
lastPoint = location;
[self setNeedsDisplayInRect:CGRectMake(0, 0, 320, 480)];
}
- (void)drawRect:(CGRect)rect {
context = UIGraphicsGetCurrentContext();
for( Line *eachLine in lineArray )
[eachLine drawInContext:context];
}
我想你可以看看如何把它充实到你需要的东西
另一种方法是使用CALayers。使用这种方法,您根本不需要在内部绘制---(void)drawRect-您可以添加和删除层,在层内绘制您喜欢的内容,视图将处理将层合成在一起并根据需要绘制到屏幕上。可能更多的是您正在寻找的内容。每次调用
drawRect
时,您都会从一张白板开始。如果你没有记录你以前画过的所有东西以便再次画,那么你最后只画了最近一次的手指,而没有画任何一次旧的手指。每次调用drawRect
时,您必须跟踪所有手指滑动,以便重新绘制它们 您可以将图像绘制成一幅图像,然后在drawRect:
方法中显示图像,而不是重画每一行。图像将为您累积线条。当然,这种方法使得撤销更难实现
从:
使用UIGraphicsBeginImageContext
函数创建一个新的基于图像的
图形上下文。创建此
在上下文中,可以绘制图像
内容,然后使用
UIGraphicsGetImageFromCurrentImageContext
函数生成基于
你画的东西。(如果需要,您可以
甚至可以继续绘制并生成
其他图像。)完成后
创建图像时,请使用
UIGraphicsSendImageContext函数
关闭图形上下文。如果你
喜欢使用核心图形,你可以
使用CGBitmapContextCreate函数
创建位图图形上下文的步骤
并将您的图像内容绘制到其中。
完成绘制后,请使用
CGBitmapContextCreateImage函数,用于
从位图创建CGImageRef
上下文你可以画核心
直接用图形图像还是用这个呢
初始化UIImage对象
我想你指的是CALayers而不是UILayers。他仍然需要在CALayer的drawInContext:method或delegate-drawLayer:inContext:method中进行一些自定义绘制,但您是对的,这些层将保留其内容,直到重新绘制。