Dam*_*ycz 6 3d animation ios scenekit
我有一个动态创建SCNView的视图.它的场景是空的,但是当我按下一个按钮时,我想从单独的scn文件中添加一个节点.这个文件包含动画,我想在主场景中设置动画.问题是在将对象添加到场景后,它不是动画.当我将此文件用作SCNView场景时,它可以正常工作.isPlaying和循环已启用.使用动画导入这样的节点还需要做什么?示例代码如下:
override func viewDidLoad() {
super.viewDidLoad()
let scene = SCNScene()
let sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
sceneView.scene = scene
sceneView.loops = true
sceneView.isPlaying = true
sceneView.autoenablesDefaultLighting = true
view.addSubview(sceneView)
let subNodeScene = SCNScene(named: "Serah_Animated.scn")!
let serah = subNodeScene.rootNode.childNode(withName: "main", recursively: false)!
scene.rootNode.addChildNode(serah)
}
Run Code Online (Sandbox Code Playgroud)
你需要从你的场景中获取动画Serah_Animated.scn,这将是一个CAAnimation对象。然后将该动画对象添加到主场景的 rootNode。
let animScene = SCNSceneSource(url:<<URL to your scene file", options:<<Scene Loading Options>>)
let animation:CAAnimation = animScene.entryWithIdentifier(<<animID>>, withClass:CAAnimation.self)
Run Code Online (Sandbox Code Playgroud)
您可以.scn使用 Xcode 中的场景编辑器从文件中找到 animID ,如下所示。
现在您可以将动画对象添加到根节点。
scene.rootNode.addAnimation(animation, forKey:<<animID>>)
Run Code Online (Sandbox Code Playgroud)
请注意,我们正在重用 animID,这将允许您也从节点中删除动画。
scene.rootNode.removeAnimation(forKey:<<animId>>)
Run Code Online (Sandbox Code Playgroud)
animID使用以编程方式获取entriesWithIdentifiersOfClass(CAAnimation.self),当您有一堆动画而不是上面的单个动画时,或者如果您只想添加动画而不关心animID之前的动画时,这很有用。您只需要检索动画:
[childNode enumerateChildNodesUsingBlock:^(SCNNode *child, BOOL *stop) {
for(NSString *key in child.animationKeys) { // for every animation key
CAAnimation *animation = [child animationForKey:key]; // get the animation
animation.usesSceneTimeBase = NO; // make it system time based
animation.repeatCount = FLT_MAX; // make it repeat forever
[child addAnimation:animation forKey:key]; // animations are copied upon addition, so we have to replace the previous animation
}
}];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5323 次 |
| 最近记录: |