我正在使用ARkit构建应用程序,我希望用户能够通过使用ViewController中的按钮来启动和停止场景中的动画。但是,我很难找到此代码的示例。任何指针将不胜感激。供参考,我的代码如下,用于对dae文件进行动画处理。我有一个可以停止动画但无法重新启动的版本。提前致谢。
func loadModels(atom:String){
// Create a new scene
let scene = SCNScene(named: "art.scnassets/" + atom + ".scn")!
// Set the scene to the view
sceneViewAr?.scene = scene
let mainnode = scene.rootNode.childNode(withName: "mainnode", recursively: true)
mainnode?.runAction(SCNAction.rotateBy(x: 10, y: 0, z: 0, duration: 0))
let orbit = scene.rootNode.childNode(withName: "orbit", recursively: true)
orbit?.runAction(SCNAction.rotateBy(x: 0, y: 2 * 50, z: 0, duration: 100))
if let orbittwo = scene.rootNode.childNode(withName: "orbit2", recursively: true) {
orbittwo.runAction(SCNAction.rotateBy(x: 0, y: -2 * 50, z: 0, duration: 100))
}
if let orbitthree = scene.rootNode.childNode(withName: "orbit3", recursively: true) {
orbitthree.runAction(SCNAction.rotateBy(x: 0, y: 2 * 50, z: 0, duration: 100))
}
}
答案 0 :(得分:0)
我认为没有一种可以按照自己想要的方式暂停和停止的方法。
话虽如此,您可以使用以下SCNAction
函数:
func action(forKey key: String) -> SCNAction?
func removeAction(forKey key: String)
基本上,您将必须停止然后再次重新创建动作。
这样,您可以执行以下操作:
在类声明下为SCNActions
创建一个变量,这样您不必每次都重写它们,例如:
let rotateXAction = SCNAction.rotateBy(x: 10, y: 0, z: 0, duration: 10)
然后创建2个功能,一个添加,另一个删除操作,例如:
/// Adds An SCNAction To A Specified Node With A Key
///
/// - Parameters:
/// - action: SCNAction
/// - node: SCNNode
/// - key: String
func addAction(_ action: SCNAction, toNode node: SCNNode, forKey key: String){
node.runAction(action, forKey: key)
}
/// Removes An SCNAction For A Specified Node With A Key
///
/// - Parameters:
/// - action: SCNAction
/// - node: SCNNode,
/// - key: String
func removeActionFromNode(_ node: SCNNode, forKey key: String){
node.removeAction(forKey: key)
}
然后您可以像这样进行测试:
/// Tests Whether The `SCNActions` Can Be Stated & Stopped
func testActions(){
//1. Create An Array Of Colours
let colours: [UIColor] = [.red, .green, .blue, .orange, .purple, .black]
//2. Create An Array Of Face Materials
var faceArray = [SCNMaterial]()
//3. Create An SCNNode With An SCNBox Geometry
let firstNode = SCNNode(geometry: SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0))
for faceIndex in 0..<colours.count{
let material = SCNMaterial()
material.diffuse.contents = colours[faceIndex]
faceArray.append(material)
}
firstNode.geometry?.materials = faceArray
//4. Add It To The Scene
self.augmentedRealityView.scene.rootNode.addChildNode(firstNode)
firstNode.position = SCNVector3(0 , 0, -1)
//5. Run The Action
addAction(rotateXAction, toNode: firstNode, forKey: "rotateX")
//6. Stop It After 4 Seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
self.removeActionFromNode(firstNode, forKey: "rotateX")
}
//7. Start It Again After 8
DispatchQueue.main.asyncAfter(deadline: .now() + 8) {
self.addAction(self.rotateXAction, toNode: firstNode, forKey: "rotateX")
}
}
根据您的情况,您可以轻松地调整这些功能以申请特定的IBAction
等。
这只是一个开始,绝不是重构或完善的示例,但是它应该为您指明正确的方向...