2011-10-18 6 views
0

私はコアグラフィックを学んでいます。 私はUIViewクラスを含む新しいプロジェクトを作成しました。 今、私は私のUIViewの実装ファイルiphoneの画面でcontiousな線を描く方法

-(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

参照してください「既存のビューに描画するのdrawRectを使用する方法は?」 http://stackoverflow.com/questions/6724800/how-to-use-drawrect-to-draw-in-a-existing-view/6725315#6725315 –

答えて

0

はとそこにコンテキスト(ライン)を保存し、背景としてUIImageViewを設定します。

CGContextStrokePath(UIGraphicsGetCurrentContext()); 
<your imageView>.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
関連する問題