我有一个 CAKeyframeAnimation 调用 animationDidStart: 和 animationDidStop:finished:,正如预期的那样,当动画被允许运行时。
当我在中断期间暂停动画(通话、按下 home btn 等)时,animationDidStop:finished: 会触发 NO 标志,正如预期的那样。
但是,重新启动暂停的动画后,animationDidStart: 和 animationDidStop:finished: 永远不会再次触发。动画确实在视觉上继续并完成,但我没有收到该事件。我需要知道动画何时停止,以便重新开始我的游戏循环。
我按照这个暂停/重新启动动画。
请告诉我我做错了什么。谢谢!
- (void)takeFlight
{
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = FLY_ANIM_TIME;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
pathAnimation.delegate = self;
CGPoint endPoint = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/3);
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, xPos, yPos);
CGPathAddQuadCurveToPoint(curvedPath, NULL, -100.0, -50.0, 200.0, 100.0);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
[shipView.layer addAnimation:pathAnimation forKey:@"flyin"];
shipView.center = endPoint;
}
-(void)pauseFlight
{
CALayer *layer = (CALayer *)shipView.layer;
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}
-(void)resumeFlight
{
CALayer *layer = (CALayer *)shipView.layer;
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}
- (void)animationDidStart:(CAAnimation *)animation
{
NSLog(@"anim did start");
}
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag
{
NSLog(@"anim did stop, flag is: %d", flag);
}