如何在iphone屏幕上绘制连续线
我只是在学习核心图形。 所以我创建了一个新项目,包括一个UIView类。 现在我的UIView实现文件中有以下代码如何在iphone屏幕上绘制连续线,iphone,core-graphics,Iphone,Core Graphics,我只是在学习核心图形。 所以我创建了一个新项目,包括一个UIView类。 现在我的UIView实现文件中有以下代码 -(id) initWithCoder:(NSCoder*)sourceCoder { if( ( self = [super initWithCoder:sourceCoder])) { //place any other initialization here } return self; } - (void)drawRect
-(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];
}
但它只是创建了一条线,如果我再次尝试绘制,那么上一条线就丢失了
但是我希望前一行不会丢失,我如何才能做到这一点?将视图设置为
NO
将UIImageView设置为背景,将上下文(行)保存在那里:
CGContextStrokePath(UIGraphicsGetCurrentContext());
<your imageView>.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGContextStrokePath(UIGraphicsGetCurrentContext());
.image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
没有成功,它仍然只画第一行。在后续绘图中,上一个绘图丢失。请参阅“如何使用drawRect在现有视图中绘图?”