こんにちは私はこのプロジェクトで、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を追加する必要がありますか?私はそれについて何も学んでいないので、私はそれを使用することを避けようとしていました。しかし、それが必要ならば、私は試してみましょう。
私が考えている別のアプローチは、これらのポイント位置の値をどこかに保存する必要があるため、すべてのポイント位置を可変配列に保存することです。しかし、浮動小数点値を配列に格納する方法が見つからないため、配列が適切かどうかはわかりません。もし誰かがこれを手伝ってくれると大変助かります。ありがとうございました!
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 –
私は、ジェスチャ認識機能を使用することをお勧めします。これは、コードを簡単にして拡張しやすくします。 – Sven
@rokjarcはい、リンクされた質問は非常に役に立ちます。もう少し小さな質問です。その答えで使用されるクラスはUIImageViewです。したがって、[self.image drawInRect:CGRectMake(0、0、self.frame.size.width、self.frame.size.height)]メソッドで呼び出されます。しかし私の場合はUIViewなので、どのようなメソッドを呼び出す必要がありますか? (申し訳ありませんが、それは愚かな質問だと思います。私は本当にnewbです。)ありがとう! – user1299082