SCNAction

学习目标

  1. 了解SceneKit游戏框架中包含的行为动画种类
  2. 掌握常用的行为动画

都有哪些动画行为

1.移动

a.移动相对于当前位置

+ (SCNAction *)moveByX:(CGFloat)deltaX y:(CGFloat)deltaY z:(CGFloat)deltaZ duration:(NSTimeInterval)duration;
+ (SCNAction *)moveBy:(SCNVector3)delta duration:(NSTimeInterval)duration;

b.移动到指定的位置

+ (SCNAction *)moveTo:(SCNVector3)location duration:(NSTimeInterval)duration;

2旋转

a.相对于当前位置旋转

+ (SCNAction *)rotateByX:(CGFloat)xAngle y:(CGFloat)yAngle z:(CGFloat)zAngle duration:(NSTimeInterval)duration;
+ (SCNAction *)rotateByAngle:(CGFloat)angle aroundAxis:(SCNVector3)axis duration:(NSTimeInterval)duration;

b. 旋转到指定的位置

+ (SCNAction *)rotateToX:(CGFloat)xAngle y:(CGFloat)yAngle z:(CGFloat)zAngle duration:(NSTimeInterval)duration;
+ (SCNAction *)rotateToX:(CGFloat)xAngle y:(CGFloat)yAngle z:(CGFloat)zAngle duration:(NSTimeInterval)duration shortestUnitArc:(BOOL)shortestUnitArc;
+ (SCNAction *)rotateToAxisAngle:(SCNVector4)axisAngle duration:(NSTimeInterval)duration;

3.缩放

a.相对于当前的尺寸缩放

+ (SCNAction *)scaleBy:(CGFloat)scale duration:(NSTimeInterval)sec;

b.缩放到指定的比例

+ (SCNAction *)scaleTo:(CGFloat)scale duration:(NSTimeInterval)sec;

4.透明度

a.透明度增加到1

+ (SCNAction *)fadeInWithDuration:(NSTimeInterval)sec;

b.透明减小到0

+ (SCNAction *)fadeOutWithDuration:(NSTimeInterval)sec;

c.透明度逐渐递增

+ (SCNAction *)fadeOpacityBy:(CGFloat)factor duration:(NSTimeInterval)sec;

d.透明度逐渐递减

+ (SCNAction *)fadeOpacityTo:(CGFloat)opacity duration:(NSTimeInterval)sec;

5.隐藏或不隐藏(让节点隐藏或者不隐藏)

 + (SCNAction *)hide NS_AVAILABLE(10_11, 9_0);
 + (SCNAction *)unhide NS_AVAILABLE(10_11, 9_0);

6.等待

a.等待指定时间

+ (SCNAction *)waitForDuration:(NSTimeInterval)sec;

b.等待随机时间

 + (SCNAction *)waitForDuration:(NSTimeInterval)sec withRange:(NSTimeInterval)durationRange;

7.从父节点移除子节点

+ (SCNAction *)removeFromParentNode;

特殊函数介绍

a.让行为相反

- (SCNAction *)reversedAction;

b.让行为永久执行

+ (SCNAction *)repeatActionForever:(SCNAction *)action;

c.让行为执行N次

+ (SCNAction *)repeatAction:(SCNAction *)action count:(NSUInteger)count;

d.把多个行为放在数组中一个一个执行

+ (SCNAction *)sequence:(NSArray<SCNAction *> *)actions;

e.把多个行为进行捆绑 一次执行

+ (SCNAction *)group:(NSArray<SCNAction *> *)actions;

f.执行代码块

 + (SCNAction *)runBlock:(void (^)(SCNNode *node))block;
 + (SCNAction *)runBlock:(void (^)(SCNNode *node))block queue:(dispatch_queue_t)queue;

自定义动画介绍

+ (SCNAction *)customActionWithDuration:(NSTimeInterval)seconds actionBlock:(void (^)(SCNNode *node, CGFloat elapsedTime))block;

javaScript动画函数介绍

a.创建一个行为执行一个javaScript程序

+ (SCNAction *)javaScriptActionWithScript:(NSString *)script duration:(NSTimeInterval)seconds;

在节点位置播放声音

+ (SCNAction *)playAudioSource:(SCNAudioSource *)source waitForCompletion:(BOOL)wait NS_AVAILABLE(10_11, 9_0);
func setup() {
    let scnView = SCNView(frame: view.bounds)
    scnView.backgroundColor = UIColor.darkGray
    view.addSubview(scnView)
    scnView.allowsCameraControl = true

    let scene = SCNScene()
    scnView.scene = scene

    let camera = SCNCamera()
    let cameraNode = SCNNode()
    cameraNode.camera = camera
    cameraNode.position = SCNVector3Make(0, 0, 50)
    scnView.scene?.rootNode.addChildNode(cameraNode)

    let cube = SCNBox(width: 10, height: 10, length: 10, chamferRadius: 0)
    let cubeNode = SCNNode(geometry: cube)
    cubeNode.geometry?.firstMaterial?.diffuse.contents = "art.scnassets/2.jpg"
    scnView.scene?.rootNode.addChildNode(cubeNode)
    let moveActions = [SCNAction.moveBy(x: 10, y: 0, z: 0, duration: 1.0),
                       SCNAction.moveBy(x: 0, y: 0, z: 10, duration: 1.0),
                       SCNAction.moveBy(x: 0, y: 10, z: 0, duration: 1.0),
                       SCNAction.move(to: SCNVector3Make(0, 0, 0), duration: 2.0)]
    let sequenceAction = SCNAction.sequence(moveActions)
    let repeatForeverAction = SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: 1, z: 0, duration: 0.5))
    let groupAction = [sequenceAction,repeatForeverAction]
    cubeNode.runAction(SCNAction.group(groupAction))
}

results matching ""

    No results matching ""