私はアプリを作成していて、一部では手書きを実装しようとしています。imageViewを使用して書き込みを行いました。これは保存され、PDFとしてサーバーに送信されます。touch begin、move、endを実装し、contextAddLineToPointを使用して、ユーザーがコンテンツを作成するときに行を作成できます。でも。文章は少し先のとがったもので、ユーザーが文章の方向を変えたとき、つまり手紙を書いたときに曲線を作成しようとしています。手書き認識を実装するのではなく、描画される線を滑らかにするためです。
配列で構成される「バッファ」を作成しました。これは、ユーザーがタッチを動かしたときに2つの中間点を保持します。次のように:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:drawImage];
NSLog(@"first touch:%@",[NSValue valueWithCGPoint:lastPoint]);
}
drawImageは、ところで書き込みに使用しているimageViewです。次に、タッチ移動に移動します。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:drawImage];
if (movementCount<2) {
[pointHolderArray addObject:[NSValue valueWithCGPoint:currentPoint]];
movementCount ++;
NSLog(@"pointHolderArray: %@",pointHolderArray);
}else
{
NSLog(@"buffer full");
UIGraphicsBeginImageContext(drawImage.frame.size);
[drawImage.image drawInRect:drawImage.frame];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.3, 0.5, 0.2, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),[[pointHolderArray objectAtIndex:0]CGPointValue].x,[[pointHolderArray objectAtIndex:0]CGPointValue].y);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), [[pointHolderArray objectAtIndex:0]CGPointValue].x,[[pointHolderArray objectAtIndex:0]CGPointValue].y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), [[pointHolderArray objectAtIndex:1]CGPointValue].x,[[pointHolderArray objectAtIndex:1]CGPointValue].y);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), [[pointHolderArray objectAtIndex:1]CGPointValue].x,[[pointHolderArray objectAtIndex:1]CGPointValue].y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = [touch previousLocationInView:drawImage];
[pointHolderArray removeAllObjects];
movementCount=0;
}
}
ご覧のとおり、2つのポイントが保存されるたびに、それらの間に線を引きます。これにより、描画が少し難しくなり、線がさらに不規則になります。
誰かが問題を解決できますか?私はiOSのグラフィックスに非常に慣れていないので、適切なAPIを使用しているかどうか、imageViewを使用する必要があるかどうかさえわかりません。
よろしくお願いします