【发布时间】:2020-12-13 09:07:01
【问题描述】:
我正在编写一个以 3D 形式显示化学反应和分子的应用程序。我从文本文件中读取了每个原子的所有值和位置,并使用 SCNSpheres 创建了每个原子形状。我有我需要正确读取的所有其他值,但我不知道如何为场景中的每个节点对象添加关键帧动画。
我在 ViewController.swift 中设置了这样的分子
func makeAtom(atomName: String, coords: [Double], scene: SCNScene) {
guard let radius = atomRadii[atomName]?.atomicRadius else { return }
atoms.append(Atom(name: atomName, x: coords[0], y: coords[1], z: coords[2], radius: radius, positions: []))
let atomGeometry = SCNSphere(radius: CGFloat(radius))
let atomNode = SCNNode(geometry: atomGeometry)
atomNode.position = SCNVector3(coords[0], coords[1], coords[2])
scene.rootNode.addChildNode(atomNode)
atomNodes.append(atomNode)
}
我知道 CAKeyframeAnimations 应该是这样设置的
let animation = CAKeyframeAnimation()
animation.keyPath = "position.y"
animation.values = [0, 300, 0]
animation.keyTimes = [0, 0.5, 1]
animation.duration = 2
animation.isAdditive = true
vw.layer.add(animation, forKey: "move")
我只是不知道我应该在哪里声明这些动画以及图层如何影响所有这些。我应该将动画添加到哪个图层?我怎样才能触发他们玩?我一直在整个互联网上搜索这方面的帮助,但我找不到任何只显示简单实现的东西。
如果需要,我可以提供更多代码,我是 StackOverflow 的新手,我想确保我做对了。
【问题讨论】:
标签: swift animation scenekit keyframe