2011-10-18 108 views
0

我只是在學習核心圖形。 所以我創建了一個新的項目,包括一個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]; 
} 

下面的代碼,但它只是在創建訂單,如果我再次嘗試繪製,然後前行丟失。

但我想上一行不應該丟失,我該怎麼做?

+0

見http://stackoverflow.com/questions/6724800/how-to-use-draw- off- draw-in-a-existing-view/6725315#6725315 –

回答

0

設置一個UIImageView爲背景,在那裏保存上下文(系):「如何使用的drawRect在現有的視圖中繪製」

CGContextStrokePath(UIGraphicsGetCurrentContext()); 
<your imageView>.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext();