21

CAKeyframeAnimationを使用して、CGPathに沿ったビューをアニメーション化しています。アニメーションが完成したら、他のメソッドを呼び出して別のアクションを実行できるようにしたいと思います。これを行う良い方法はありますか?

UIViewのsetAnimationDidStopSelectorの使用を見てきましたが、ドキュメントからは、UIViewアニメーションブロック(beginAnimationsおよびcommitAnimations)内で使用された場合にのみ適用されるように見えます。念のため試してみましたが、うまくいかないようです。

次にいくつかのサンプルコードを示します(これはカスタムUIViewサブクラスメソッド内にあります)。

// These have no effect since they're not in a UIView Animation Block
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];    

// Set up path movement
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"path"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 1.0f;

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, self.center.x, self.center.y);

// add all points to the path
for (NSValue* value in myPoints) {
    CGPoint nextPoint = [value CGPointValue];
    CGPathAddLineToPoint(path, NULL, nextPoint.x, nextPoint.y);
}

pathAnimation.path = path;
CGPathRelease(path);

[self.layer addAnimation:pathAnimation forKey:@"pathAnimation"];

私が考えていた回避策は機能するはずですが、最善の方法ではないようですが、NSObjectのperformSelector:withObject:afterDelay:を使用することです。遅延をアニメーションの長さに等しく設定する限り、問題はありません。

もっと良い方法はありますか?ありがとう!

4

3 に答える 3

37

または、アニメーションを次のように囲むことができます。

[CATransaction begin];
[CATransaction setCompletionBlock:^{
                   /* what to do next */
               }];
/* your animation code */
[CATransaction commit];

そして、あなたがする必要があることを処理するために完了ブロックを設定します。

于 2012-06-19T09:39:40.357 に答える
24

CAKeyframeAnimationは、CAAnimationのサブクラスです。CAAnimationにdelegateプロパティがあります。デリゲートは-animationDidStop:finished:メソッドを実装できます。残りは簡単なはずです。

于 2010-03-18T07:46:36.980 に答える
7

この回答のSwift3構文。

CATransaction.begin()
CATransaction.setCompletionBlock {
    //Actions to be done after animation
}
//Animation Code
CATransaction.commit()
于 2017-01-24T10:40:10.537 に答える