我正在嘗試通過觸摸它來在屏幕上繪製形狀的應用程序。繪製形狀時線條被擦除
我可以從一個點畫一條線到另一個點,但是它會在每個新的平局上消除。
這裏是我的代碼:
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