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:を使用することです。遅延をアニメーションの長さに等しく設定する限り、問題はありません。
もっと良い方法はありますか?ありがとう!