SCNAction : 动画
- SCNAction: 行为动画
- 移动
- 旋转,
- 缩放,
- 透明度,
- 隐藏或者不隐藏,
- 等待,
- 节点删除
- 上代码,看效果
- (void)viewDidLoad {
[super viewDidLoad];
//1. 创建View
SCNView *scnView = [[SCNView alloc] initWithFrame:self.view.bounds];
scnView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:scnView];
//2. 创建场景
SCNScene *scene = [SCNScene scene];
scnView.scene = scene;
// 3. 创建球体
SCNSphere *sphere = [SCNSphere sphereWithRadius:0.5];
sphere.firstMaterial.diffuse.contents = @"earth.jpg";
SCNNode *earthNode = [SCNNode nodeWithGeometry:sphere];
[scene.rootNode addChildNode:earthNode];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 移动
SCNAction *moveAction = [SCNAction moveTo:SCNVector3Make(0, 1, 0) duration:1.f];
SCNAction *moveByAction = [SCNAction moveBy:SCNVector3Make(0, 1, 0) duration:1.f];
SCNAction *moveByX = [SCNAction moveByX:1 y:1 z:1 duration:2];
// 旋转
SCNAction *roteAction = [SCNAction rotateByX:1 y:1 z:0 duration:2];
SCNAction *rotebyangelAction = [SCNAction rotateByAngle:10 aroundAxis:SCNVector3Make(0, 1, 0) duration:2];
// 放大
SCNAction *scale = [SCNAction scaleTo:1.2 duration:1];
SCNAction *scale1 = [SCNAction scaleTo:1. duration:1];
// 透明度
// SCNAction *fadeinAction = [SCNAction fadeInWithDuration:2];
SCNAction *fadeoutAction = [SCNAction fadeOutWithDuration:2];
SCNAction *fadetoAction = [SCNAction fadeOpacityTo:0.2 duration:1];
SCNAction *fadebyAction = [SCNAction fadeOpacityBy:1 duration:1];
//隐藏,显示
SCNAction *hiddenMoon = [SCNAction hide];
SCNAction *unhiddenMoon = [SCNAction unhide];
// 把多个行为放到一个数组中一个一个执行
SCNAction *seque = [SCNAction sequence:@[fadetoAction,fadebyAction]];
SCNAction *seque1 = [SCNAction sequence:@[scale,scale1]];
//把多个行为捆绑到一起执行
SCNAction *group = [SCNAction group:@[seque,rotebyangelAction,seque1]];
SCNAction *runever = [SCNAction repeatActionForever:group];
[earthNode runAction:runever];
});
// 运行操作运动摄像机
scnView.allowsCameraControl = YES;
// 抗锯齿(会比较消耗系统性能)
scnView.antialiasingMode = SCNAntialiasingModeMultisampling4X;
// 添加一个相机节点
SCNNode *cameraNode = [SCNNode node];
SCNCamera *camera = [SCNCamera camera];
camera.automaticallyAdjustsZRange = YES;
cameraNode.camera = camera;
cameraNode.position = SCNVector3Make(0, 0, 5);
[scene.rootNode addChildNode:cameraNode];
// // 设置游戏帧率
scnView.preferredFramesPerSecond = 60;
// 打开统计菜单
scnView.showsStatistics = YES;
scnView.debugOptions = SCNDebugOptionShowLightInfluences;
}
效果: