1

iPhoneの画面上で指の動きの軌跡を保存したい。今のところ、タッチを読んでCGPointsをNSMutableArrayに追加しています。その配列内のすべての cgpoint を印刷しようとすると、中間点が欠落しています。これにもっと良い方法はありますか?パス全体を直接保存できますか?

これが私が使用しているコードです

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

fingerSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
[self.myPoints addObject:[NSValue valueWithCGPoint:lastPoint]];
  }


 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
   {
fingerSwiped = YES;

UITouch *touch = [touches anyObject];   
CGPoint currentPoint = [touch locationInView:self.view];



UIGraphicsBeginImageContext(self.view.frame.size);
[slateImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
//CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0,0,0, 1.0);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.drawcolor.CGColor);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
slateImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
[myPoints addObject:[NSValue valueWithCGPoint:lastPoint]];   
}

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

if(!fingerSwiped) 
{
    UIGraphicsBeginImageContext(self.view.frame.size);
    [slateImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
    //CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0,0,0, 1.0);
    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.drawcolor.CGColor);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    CGContextFlush(UIGraphicsGetCurrentContext());
    slateImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [myPoints addObject:[NSValue valueWithCGPoint:lastPoint]];
}
}
4

1 に答える 1

6

iOS が喜んで提供するすべてのポイントを記録しています。

iOS は、16.67 ミリ秒 (1 秒あたり 60 回) ごとにタッチ移動イベントのみを報告します。それよりも速く更新されたタッチ位置を取得する方法は (私が知る限り) ありません。

タッチポイントをプロットすると、直線が得られると言います。これは、ユーザーが指を非常に速く動かしているため、タッチが 16.67 ミリ秒でかなりの量移動するために発生しています。更新と更新の間にタッチが大きく移動しているため、ポイントを接続すると滑らかな曲線に見えません。残念ながら、(前述したように) 1 秒あたり 60 回よりも高速に更新を取得する方法はありません。

これに対処する唯一の方法は、スプライン補間を使用してレポートされたポイントを接続することです。スプライン補間は複雑な問題です。Google を使用すると、それに関する多くの情報を見つけることができます。

この例は、iPad の Adob​​e Ideas アプリで確認できます。大きならせんを速く描いて注意深く見ると、指を離すと線が滑らかになることがわかります。らせんを描いているときに少しずつ滑らかになっていると思います。指を離すと、戻って線全体のより良い補間を計算します。

于 2012-03-01T07:35:50.483 に答える