我正在寻找一种绕着z轴旋转360度音符的方法.保持摄像机的节点沿z轴负向移动,不允许改变其y和z.我已经试过了CABasicAnimation,但没有成功.
有谁能指出我的解决方案?
沿 z 轴永远旋转对象。
let node = SCNNode()
self.sceneView.scene.rootNode.addChildNode(node)
//creating an box object
node.geometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
node.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
node.position = SCNVector3(0,0,0)
//adding a rotation by Z axis
let action = SCNAction.rotateBy(x: 0, y: 0, z: CGFloat(GLKMathDegreesToRadians(360)), duration: 8)
let forever = SCNAction.repeatForever(action)
node.runAction(forever)
Run Code Online (Sandbox Code Playgroud)
如何将其旋转小于360°?由于你的标题是"无限",它应该可以正常工作,并且一遍又一遍地重复动画将会提升360°甚至更高.
另外,你可以看看Apple默认的SceneKit项目,它对我很有用:
[cameraNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:.5 z:0 duration:1]]];
Run Code Online (Sandbox Code Playgroud)
或者,对于OSX:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"rotation"];
animation.toValue = [NSValue valueWithSCNVector4:SCNVector4Make(0, 0, 1, M_PI*2)];
animation.duration = 15;
animation.repeatCount = MAXFLOAT; //repeat forever
[YOURNODE addAnimation:animation forKey:nil];
Run Code Online (Sandbox Code Playgroud)