質問が大幅に変更され、新しい質問になったため、質問を書き直しています。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
location = [touch locationInView:self.view];
CGPoint prevLocation = [touch previousLocationInView:self.view];
CGFloat distanceFromPrevious = [self distanceBetweenPoints:location :prevLocation];
NSTimeInterval timeSincePrevious = event.timestamp - self.previousTimestamp;
if (locArray == NULL) {
locArray = [[NSMutableArray alloc] init];
}
[locArray addObject:[NSValue valueWithCGPoint:prevLocation]];
CGFloat speed = distanceFromPrevious/timeSincePrevious;
self.previousTimestamp = event.timestamp;
//NSLog(@"speed %f | time %f", speed, timeSincePrevious);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CALayer *animLayer = ball.layer;
animLayer.bounds = ball.bounds;
animLayer.position = CGPointMake(self.view.frame.size.width/2, 0);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, self.view.frame.size.width/2, 380);
for (int i=0; i<[locArray count]; i++) {
NSValue *val = [locArray objectAtIndex:i];
CGPoint p = [val CGPointValue];
CGPathAddLineToPoint(path, NULL, p.x, p.y);
NSLog(@"Points %f %d", p.y, i);
}
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"path"];
animation.duration = 4.0f;
animation.path = path;
animation.removedOnCompletion = NO;
CGPathRelease(path);
[animLayer addAnimation:animation forKey:@"path"];
locArray = nil;
}
UITouch touchesMoved イベント キャプチャに基づいて、ball という名前のクラスを表示しようとしています。LocArray は、移動したタッチの CGPoint 値を保持しています。これらの値を CGMutablePathRef に入れようとしました。それよりも、ユーザーの指の動きの入力によって作成されたパス上のボールをアニメーション化しようとしました。
アニメーションを機能させることができません。それの何が問題なのですか?
編集:
私は変わりました :
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animationWithKeyPath 値は現在位置であり、現在は機能しています:)
少し問題がありますが、解決しました。
しかし、私の新しい質問は次のとおりです。
ボール ビューは、アニメーションの完了後に開始点に戻り、停止します。
パスの最終ポイントにとどまることを望みます。
解決策:最終的に解決策を見つけました。
CGPoint finalPoint = [locArray count]-1 値を追加しました。それで、
animLayer.position = finalPoint;
それはそれをすべて解決しました。これで、ボールは touchesMoved イベントによってキャプチャされた指のタッチの最終位置にとどまります。