Bar*_*ski 5 animation ios scenekit swift scntranscaction
我希望能够在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()
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试运行多个串行动画,如下所示:
func animateMoves(_ moves: [Move]) {
for (index, move) in moves.enumerated() {
perform(#selector(animateMove(_:)),
with: move,
afterDelay: TimeInterval(Double(index) * move.duration)
}
}
Run Code Online (Sandbox Code Playgroud)
一切都是动画,但动画不是以连续的方式运行.动画在不可预测的时间内开始和结束.来自调试器的示例日志:
Animate move started 1
Animate move started 2
Animate move finished 1
Animate move finished 2
Animate move started 3
Animate move finished 3
Run Code Online (Sandbox Code Playgroud)
我意识到我的方法不是最好的,但只有通过这种方式,我才能实现几乎可以工作的动画.
我知道有一个SCNAction类可用.也许我应该在一次交易中做出很多动作?如果是这样,有人可以向我解释SCNTransactions究竟是如何工作的以及SCNTransaction的完成块在不可预测的时间内完成的原因是什么?
尝试使用SCNAction.sequence():
class func sequence([SCNAction])
Run Code Online (Sandbox Code Playgroud)
创建一个按顺序运行一组操作的操作
let sequence = SCNAction.sequence([action1, action2, action3]) // will be executed one by one
let node = SCNNode()
node.runAction(sequence, completionHandler:nil)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1901 次 |
| 最近记录: |