2012-03-01 4 views
1

iPhoneの画面に指の動きのパスを保存したい。現在、NSMutableArrayに触れてCGPointを追加するだけです。その配列内のすべてのcgpointを何点か途中で見つけられないように印刷しようとします。これには良い方法がありますか?パス全体を直接保存できますか?ここで指の動きのパスを目的のCにどのように保存できますか?

は、私はあなたがiOSのはあなたを与えて喜んですべてのポイントを記録している

- (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]]; 
} 
} 
+0

ポイントを配列に追加するコードを共有できます – dbrajkovic

+0

これまで忘れてしまったことがあります。 – slonkar

+2

なぜあなたはそれが中間点を欠いていると思いますか?また、 'touchesBegan:withEvent:'の 'myPoints'配列をクリアしますか? –

答えて

6

を使用していたコードです。

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

タッチポイントをプロットすると直線が得られると言います。これは、ユーザーが指をすばやく動かしてタッチが16.67ミリ秒でかなり動くために起こります。あなたがポイントを接続するときにタッチが滑らかな曲線のようには見えない、アップデートの間にタッチが移動しています。残念ながら、(私が言ったように)1秒間に60回以上更新を取得する方法はありません。

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

この例は、iPadのAdobe Ideasアプリで確認できます。大きなスパイラルを速く描いて慎重に見ると、指を離すと線が滑らかになることがわかります。あなたが指先を持ち上げると、それは戻って、ライン全体のより良い補間を計算します。

+1

ポインタありがとうございます。しかし、問題は、配列からポイントをプロットしている場合に限り、ポイントが欠けていることです。実際には、GKSessionを使ってブルートゥース・アドホック・ネットワークを介してそのアレイを他のデバイスに渡しており、他のデバイスでそれを再生成しようとしています。 – slonkar

関連する問題