我只是在學習核心圖形。 所以我創建了一個新的項目,包括一個UIView類。 我現在已經在我UIView實現文件如何在iphone屏幕上繪製連續線
-(id) initWithCoder:(NSCoder*)sourceCoder
{
if((self = [super initWithCoder:sourceCoder]))
{
//place any other initialization here
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context,4);
CGContextSetStrokeColorWithColor(context, myColor.CGColor);
CGContextMoveToPoint(context,startPoint.x , startPoint.y);
CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
CGContextStrokePath(context);
}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch* touchPoint = [touches anyObject];
startPoint = [touchPoint locationInView:self];
endPoint = [touchPoint locationInView:self];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
endPoint=[touch locationInView:self];
[self setNeedsDisplay];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
endPoint = [touch locationInView:self];
[self setNeedsDisplay];
}
下面的代碼,但它只是在創建訂單,如果我再次嘗試繪製,然後前行丟失。
但我想上一行不應該丟失,我該怎麼做?
見http://stackoverflow.com/questions/6724800/how-to-use-draw- off- draw-in-a-existing-view/6725315#6725315 –