我希望能够在 SceneKit 中一个接一个地运行多个动画。我已经实现了一个像这样运行一个动画的函数:
fileprivate func animateMove(_ move: Move) {
print("Animate move started " + move.identifier)
// I am creating rotateNode
let rotateNode = SCNNode()
rotateNode.eulerAngles.x = CGFloat.pi
scene.rootNode.addChildNode(rotateNode)
// Then I am selecting nodes which I want to rotate
nodesToRotate = ...
// Then I am adding the nodes to rotate node
_ = nodesToRotate.map { rotateNode.addChildNode($0) }
SCNTransaction.begin()
SCNTransaction.animationDuration = move.animationDuration
SCNTransaction.completionBlock = {
rotateNode.enumerateChildNodes { node, _ in
node.transform = node.worldTransform
node.removeFromParentNode()
scene.rootNode.addChildNode(node)
}
rotateNode.removeFromParentNode()
print("Animate move finished " + move.identifier)
}
SCNTransaction.commit()
}
然后我尝试像这样运行多个连续动画:
func animateMoves(_ moves: [Move]) {
for (index, move) in moves.enumerated() {
perform(#selector(animateMove(_:)),
with: move,
afterDelay: TimeInterval(Double(index) * move.duration)
}
}
一切都是动画,但动画不是连续运行的。动画的开始和结束时间不可预测。 来自调试器的示例日志:
Animate move started 1
Animate move started 2
Animate move finished 1
Animate move finished 2
Animate move started 3
Animate move finished 3
我意识到我的方法不是最好的,但只有这样我才能实现几乎可以工作的动画。
我知道有一个 SCNAction 类可用。也许我应该在一次交易中进行多项操作?如果是这样,有人可以向我解释 SCNTransactions 究竟是如何工作的以及为什么完成 SCNTransaction 的完成 block 会在不可预测的时间内触发吗?
最佳答案
尝试使用SCNAction.sequence():
class func sequence([SCNAction])
创建一个按顺序运行一组 Action 的 Action
let sequence = SCNAction.sequence([action1, action2, action3]) // will be executed one by one
let node = SCNNode()
node.runAction(sequence, completionHandler:nil)
关于ios - SceneKit 中的多个串行动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42841273/