0

iOS コア アニメーション「CABasicAnimation」を使用して UIImageView をアニメーション化していますが、すべて正常に動作しますが、唯一の問題は、その位置にアニメーション化すると、アニメーションが完了した後、元の位置に戻ることです。どうすればこれを克服できますか?UIImageView を移動した位置に保持する必要があります。

注:これに関して成功の答えがある質問はほとんど見たことがありませんが、彼らが言うように機能しない理由がわかりません。

CABasicAnimation を使用して CALayer を回転させた後、レイヤーは回転していない位置に戻ります。

ここに私のサンプルコードがあります、

    CGPoint endPt = CGPointMake(160, 53);
    CABasicAnimation *anim5 = [CABasicAnimation animationWithKeyPath:@"position"];
    [anim5 setBeginTime:CACurrentMediaTime()+0.4];
    anim5.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    anim5.fromValue = [NSValue valueWithCGPoint:imgRef.layer.position];
    anim5.toValue = [NSValue valueWithCGPoint:endPt];
    anim5.duration = 1.5;
    anim5.speed = 2;
    [imgRef.layer addAnimation:anim5 forKey:@"position"];

    //this is what other answers ask to do
    [anim5 setFillMode:kCAFillModeForwards];
    [anim5 setRemovedOnCompletion:NO];

ところで[imgRef.layer setPosition:CGPointMake(160, 53)];、私はアニメーションを 4 ミリ秒遅らせているので、役に立ちません。

4

2 に答える 2

3

根本的な原因は、アニメーションがプロパティを 2 つの値の間で遷移させるだけであり、実際には終了値を変更しないことです。アニメーションが完了したら、終了値を変更する必要があります。それには 3 つの方法があります。1) CAAnimation スーパークラスのデリゲート プロパティを使用して、アニメーションの完了時に通知を受け取ります。その時点で、プロパティをその終了値に設定できます。参照: https://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CAAnimation_class/Introduction/Introduction.html#//apple_ref/occ/cl/CAAnimationanimationDidStop:finished: は、デリゲートに実装する必要があるメソッドです。2) 周囲の CATransaction に完了ブロックを設定します。CABasicAnimation で自動的に開始するのではなく、手動で CATransaction を開始する必要があります。参照: Objective-C - アニメーション後に変更を適用する CABasicAnimation? 3) 以下の OMZ のコメントを参照してください...

于 2013-04-01T12:56:23.253 に答える
1

正しい答えは、レイヤーの位置プロパティを設定することですが、指摘したように、位置変更の前に 0.4 秒の遅延が必要なため、より困難になります。最初に遅延を実行してからアニメーションを実行できなかった理由はありますか? このようなもの:

- (IBAction)didTapMove:(id)sender
{
  [self performSelector:@selector(animate) withObject:nil afterDelay:0.4];
}

- (void)animate
{
  CGPoint endPt = CGPointMake(160, 53);
  CABasicAnimation *anim5 = [CABasicAnimation animationWithKeyPath:@"position"];
  anim5.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  anim5.fromValue = [NSValue valueWithCGPoint:_imageView.layer.position];
  anim5.toValue = [NSValue valueWithCGPoint:endPt];
  anim5.duration = 1.5;
  anim5.speed = 2;

  [_imageView.layer addAnimation:anim5 forKey:@"position"];

  [_imageView.layer setPosition:CGPointMake(160, 53)];
}

実行セレクター呼び出しで遅延が発生しているため、アニメーションから開始時間を削除したことに気付きました。

于 2013-04-01T16:35:08.583 に答える