2012-03-28 12 views
2

こんにちは私はこのプロジェクトで、UIViewでのユーザーの落書きを許可しています。私のアプローチは、TouchMovedの間にCGMutablePathRefパスを作成し、新しい行を追加することです。UIViewでのdoodle - iosの開発

関連するコードは、UIViewのクラスにあります。

static CGMutablePathRef path; //create it as static value. 
//I got this habit from java coding but actually not sure 
//if it's a good way to do it in objective-c. 


//draw the path 

-(void)drawRect:(CGRect)rect{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextBeginPath(context); 
    CGContextAddPath(context, path); 
    CGContextStrokePath(context); 
    CGPathRelease(path); 
} 

//touch began. create the path and add point. 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 

    path = CGPathCreateMutable(); 
    UITouch *touch = [touches anyObject]; 
    CGPoint point = [touch locationInView:self]; 
    CGPathMoveToPoint(path, NULL, point.x, point.y); 
    [self setNeedsDisplay];    
} 


//add points when touch moved 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 

    UITouch *touch = [touches anyObject]; 
    CGPoint point = [touch locationInView:self]; 

    CGPathAddLineToPoint(path, NULL, point.x, point.y); 
    [self setNeedsDisplay]; 

} 

//last points when touch ends 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint point = [touch locationInView:self]; 
    CGPathAddLineToPoint(path, NULL, point.x, point.y); 
    [self setNeedsDisplay]; 
} 

はしかし、私はエラーを持って、私は本当に私はそれは私が他の落書きコードから見たUIGestureRecognizer、と何かだと思う...それを理解していませんでした。 UIGestureRecognizerを追加する必要がありますか?私はそれについて何も学んでいないので、私はそれを使用することを避けようとしていました。しかし、それが必要ならば、私は試してみましょう。

私が考えている別のアプローチは、これらのポイント位置の値をどこかに保存する必要があるため、すべてのポイント位置を可変配列に保存することです。しかし、浮動小数点値を配列に格納する方法が見つからないため、配列が適切かどうかはわかりません。もし誰かがこれを手伝ってくれると大変助かります。ありがとうございました!

+0

UIGestureRecognizersを使用する必要はありません。浮動小数点数をNSMutableArrayに格納する場合は、[[NSNumber](http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html)numberWithFloat: ]。また、この質問を見て:http://stackoverflow.com/questions/7393058/how-do-i-use-uibezierpath-to-draw-a-line-that-follows-a-touch-event –

+0

私は、ジェスチャ認識機能を使用することをお勧めします。これは、コードを簡単にして拡張しやすくします。 – Sven

+0

@rokjarcはい、リンクされた質問は非常に役に立ちます。もう少し小さな質問です。その答えで使用されるクラスはUIImageViewです。したがって、[self.image drawInRect:CGRectMake(0、0、self.frame.size.width、self.frame.size.height)]メソッドで呼び出されます。しかし私の場合はUIViewなので、どのようなメソッドを呼び出す必要がありますか? (申し訳ありませんが、それは愚かな質問だと思います。私は本当にnewbです。)ありがとう! – user1299082

答えて

1

私に助けてくれた人に感謝します。幸いなことに、この問題を解決する効果的な方法を見つけました。

一般に、SketchViewのimageViewは、描画するときに動的に更新されます。つまり、ピクセルを追加するたびに、その新しいピクセルに1行が追加され、UIImageViewが変更され、UIViewの背景イメージとして設定されます。

SketchView.h

@property (assign, nonatomic) CGPoint CurrentPoint; 
@property (assign, nonatomic) CGPoint PreviousPoint; 
@property (assign, nonatomic) CGPoint InitialPoint; 

SketchView.m

@synthesize CurrentPoint; 
@synthesize PreviousPoint; 
@synthesize InitialPoint; 


//begin the touch. store the initial point because I want to connect it to the last 
//touch point 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint point = [touch locationInView:image]; 
    InitialPoint = point; 

    } 


//When touch is moving, draw the image dynamically 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 

UITouch *touch = [touches anyObject]; 
PreviousPoint = [touch previousLocationInView:image]; 
CurrentPoint = [touch locationInView:image]; 
UIGraphicsBeginImageContext(image.frame.size); 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
[image.image drawInRect:CGRectMake(0, 0, image.frame.size.width, image.frame.size.height)]; 
CGContextSetLineCap(ctx, kCGLineCapRound); 
CGContextSetLineWidth(ctx, 5.0); 
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); 
CGContextBeginPath(ctx); 
CGContextMoveToPoint(ctx, PreviousPoint.x, PreviousPoint.y); 
CGContextAddLineToPoint(ctx, CurrentPoint.x, CurrentPoint.y); 
CGContextStrokePath(ctx); 
image.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
} 



- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 

UITouch *touch = [touches anyObject]; 
PreviousPoint = [touch previousLocationInView:image]; 
CurrentPoint = [touch locationInView:image]; 
UIGraphicsBeginImageContext(image.frame.size); 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
[image.image drawInRect:CGRectMake(0, 0, image.frame.size.width, image.frame.size.height)]; 
CGContextSetLineCap(ctx, kCGLineCapRound); 
CGContextSetLineWidth(ctx, 5.0); 
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); 
CGContextBeginPath(ctx); 
CGContextMoveToPoint(ctx, PreviousPoint.x, PreviousPoint.y); 
CGContextAddLineToPoint(ctx, CurrentPoint.x, CurrentPoint.y); 

//I connected the last point to initial point to make a closed region 
CGContextMoveToPoint(ctx, CurrentPoint.x, CurrentPoint.y); 
CGContextAddLineToPoint(ctx, InitialPoint.x, InitialPoint.y); 
CGContextStrokePath(ctx); 
image.image = UIGraphicsGetImageFromCurrentImageContext(); 


UIGraphicsEndImageContext(); 
} 

そして、それは働きます!

p.s.それは私がだらだらやったコードではあまり言及していないのに、私はこれが役に立てば幸い...、

PreviousPoint = [touch previousLocationInView:image]; 

が非常に役に立ちました。 :)