1

我有一个奇怪的情况。我正在尝试运行动画,并且正在等待完成处理程序。一切正常,直到我定义了timingFunction。在那之后,有时completionHandler 根本不会被调用。我的动画卡在最后一帧。

CGFloat rotations = times * 2.0;
CGFloat zoomLevel = COIN_MOVE_UP;
SCNAction* rotate = [SCNAction rotateByX:M_PI_2 y:0 z:0 duration:2.0 / (rotations + 1)];
NSUInteger swaps = rotations / 2;
SCNAction* rotateSequence = [SCNAction repeatAction:rotate count:2];
SCNAction* coinRotate = [SCNAction repeatAction:rotateSequence count:swaps];
SCNAction* coinRotateAction = [SCNAction sequence:@[coinRotate, rotate]];

SCNAction* coinMoveUpAction = [SCNAction moveByX:0 y:-zoomLevel z:0 duration:1];
SCNAction* coinMoveDownAction = [SCNAction moveByX:0 y:zoomLevel z:0 duration:1];

SCNAction* coinMoveAction = [SCNAction sequence:@[coinMoveUpAction, coinMoveDownAction]];
SCNAction* coinRotateY = [SCNAction rotateByX:0 y:M_PI z:0 duration:2];
SCNAction* fullGroup = [SCNAction group:@[coinRotateAction, coinMoveAction, coinRotateY]];
[fullGroup setTimingFunction:^(float time){
    return time;
}];

[self.coinNode runAction:fullGroup completionHandler:^{
    // completion is not called sometimes when timingFunction is efined
}];

你知道如何解决这个问题吗?

PS我已经实现:

-(void)renderer:(id<SCNSceneRenderer>)renderer didApplyAnimationsAtTime:(NSTimeInterval)time{

}

并且没有计时功能,当动画完成时会停止调用此方法,但是当存在 timig 功能时,有时动画会一直运行到结束,但不会调用完成处理程序并且 didApplyAnimationsAtTime: 仍在被调用。所以也许这个动画在达到结束状态后仍然在运行。

4

1 回答 1

0

最后我能够通过使用解决这个问题

+ (SCNAction *)customActionWithDuration:(NSTimeInterval)seconds actionBlock:(void (^)(SCNNode *node, CGFloat elapsedTime))block;

所以我删除了所有计时功能并将它们重新实现为块:

CGFloat angle = M_PI_2;
void (^block)(SCNNode * _Nonnull node, CGFloat elapsedTime) = ^(SCNNode * _Nonnull node, CGFloat elapsedTime) {
    CGFloat progress = elapsedTime / duration;
    CGFloat realTime = pow(progress, 6); // here I apply timing
    node.eulerAngles = SCNVector3Make(initialVector.x + (angle * realTime), initialVector.y, initialVector.z);
    node.position = SCNVector3Make(node.position.x, cos(angle * realTime) * 5, sin(angle * realTime) * 5);
};

这样,我对未调用的完成处理程序没有任何问题,我仍然可以调整动画的时间:)

于 2015-11-23T13:49:26.130 回答