0

パスをたどり、パスの接線で回転するためにCALayerを使用してアニメーション化するカスタムがあります。CAAnimationGroup

   // Create the animation path
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;

//Setting Endpoint of the animation
CGRect contentBounds = [self contentBounds];
self.boatLayer.bounds = contentBounds;
CGPoint endPoint = CGPointMake(contentBounds.size.width - 150, contentBounds.size.height - 150);

CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, startPosition.x, startPosition.y);
CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, 0, endPoint.x, 0, endPoint.x, endPoint.y);
pathAnimation.path = curvedPath;

pathAnimation.duration = 10.0;
pathAnimation.rotationMode = kCAAnimationRotateAuto;
pathAnimation.delegate = self;

// Create an animation group of all the animations
CAAnimationGroup *animationGroup = [[[CAAnimationGroup alloc] init] autorelease];
animationGroup.animations = [NSArray arrayWithObjects:pathAnimation, nil];
animationGroup.duration = 10.0;
animationGroup.removedOnCompletion = NO;

// Add the animations group to the layer (this starts the animation at the next refresh cycle)
[testLayer addAnimation:animationGroup forKey:@"animation"];

レイヤーがパスに沿って進むにつれて、レイヤーの位置と回転の変化を追跡できるようにする必要があります。と の両方をオーバーライドsetPositionsetTransform( と を呼び出しsuper setPositionsuper setTranform)、それらの値をログに記録しました。これらの値はいずれも、アニメーション中に設定されているようには見えません。

CALayerクラス自体がアニメーション化されているときに、クラス自体から位置と回転の更新を取得するにはどうすればよいですか?

4

1 に答える 1

1

コアアニメーションはそのようには機能しません

ごめん。それは Core Animation の仕組みではありません。レイヤーにアニメーションを追加しても、そのレイヤーのモデルは変更されません。プレゼンのみ。

完了時にアニメーションを削除しないように構成すると、

yourAnimation.fillMode = kCAFillModeForwards;
tourAnimation.removedOnCompletion = NO;

実際には、画面に表示されているものとそのレイヤーのモデルとの間に矛盾が生じています。たとえば、このようにアニメーション化したボタンがあった場合、「タッチに反応しなくなった」またはさらに面白い「「古い」場所からのタッチに反応する」という事実に非常に驚いたり怒ったりするでしょう。

セミソリューション

実際に更新が必要な内容と頻度に応じてpresentationLayer、アニメーション中にの値を定期的にチェックするCADisplayLinkか、画面が変化したときに を使用してコードを実行することができます。

于 2013-04-03T14:01:13.773 に答える