我正在尝试通过触摸它来在屏幕上绘制形状的应用程序。绘制形状时线条被擦除
我可以从一个点画一条线到另一个点,但是它会在每个新的平局上消除。
这里是我的代码:
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;
}
感谢您的帮助 -
尼尔。
我想你的意思是CALayers而不是UILayers。他仍然需要在CALayer的drawInContext:方法或委托-drawLayer:inContext:方法中进行一些自定义绘图,但是您说得对,直到重绘后才会保留其内容。 – 2009-11-04 13:41:22
谢谢,你的权利 - 我已经赞赏了答案 – 2009-11-04 15:05:23