iOS开发之SceneKit框架--SCNAction.h

1、SCNAction简介

  主要负责节点SCNNode的属性,实现node的渐变、移动、出现、消失、实现动画等。

 

2、相关API

  • 节点的移动(earthNode的初始坐标(5,0,0))
//从当前位置移动指定的距离X轴:deltaX Y轴:deltaY Z轴:deltaZ
+ (SCNAction *)moveByX:(CGFloat)deltaX y:(CGFloat)deltaY z:(CGFloat)deltaZ duration:(NSTimeInterval)duration;
+ (SCNAction *)moveBy:(SCNVector3)delta duration:(NSTimeInterval)duration;

    [self.earthNode runAction:[SCNAction moveByX:5 y:5 z:0 duration:1]];
    [self.earthNode runAction:[SCNAction moveBy:SCNVector3Make(5, 5, 0) duration:1]]; 

 

//移动到指定坐标location
+ (SCNAction *)moveTo:(SCNVector3)location duration:(NSTimeInterval)duration;

    [self.earthNode runAction:[SCNAction moveTo:SCNVector3Make(5, 5, 0) duration:1]];
  • 节点的旋转
//当前的节点基础上进行旋转指定的角度X轴:xAngle Y轴:yAngle Z轴:zAngle
+ (SCNAction *)rotateByX:(CGFloat)xAngle y:(CGFloat)yAngle z:(CGFloat)zAngle duration:(NSTimeInterval)duration;

//旋转到指定的角度X轴:xAngle Y轴:yAngle Z轴:zAngle
//shortestUnitArc:YES是顺时针,NO为逆时针
+ (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;

    [self.earthNode runAction:[SCNAction rotateByX:0 y:M_PI_2 z:0 duration:4]];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.earthNode runAction:[SCNAction rotateToX:0 y:0 z:0 duration:4]];
    });

 

//axis和原点组成轴向量,旋转angle角度
//axisAngle:是四分量,前三个坐标相当于axis的值,第四个值为旋转角度
+ (SCNAction *)rotateByAngle:(CGFloat)angle aroundAxis:(SCNVector3)axis duration:(NSTimeInterval)duration;
+ (SCNAction *)rotateToAxisAngle:(SCNVector4)axisAngle duration:(NSTimeInterval)duration;

    [self.earthNode runAction:[SCNAction rotateByAngle:M_PI_2 aroundAxis:SCNVector3Make(5, 5, 0) duration:4]];

    [self.earthNode runAction:[SCNAction rotateToAxisAngle:SCNVector4Make(5, 5, 0, M_PI_2) duration:4]];
  •  节点的缩放
//在原来基础上进行缩放
+ (SCNAction *)scaleBy:(CGFloat)scale duration:(NSTimeInterval)sec;
//在初始化的基础上进行缩放
+ (SCNAction *)scaleTo:(CGFloat)scale duration:(NSTimeInterval)sec;

    [self.earthNode runAction:[SCNAction scaleTo:2 duration:2]];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.earthNode runAction:[SCNAction scaleBy:0.5 duration:2]];
    });

    [self.earthNode runAction:[SCNAction scaleBy:2 duration:2]];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.earthNode runAction:[SCNAction scaleTo:0.5 duration:2]];
    });
  • 节点的透明度
//透明度变换到1的动画
+ (SCNAction *)fadeInWithDuration:(NSTimeInterval)sec;
//透明度变换到0的动画
+ (SCNAction *)fadeOutWithDuration:(NSTimeInterval)sec;
//原来的透明度基础上 变换值为factor
+ (SCNAction *)fadeOpacityBy:(CGFloat)factor duration:(NSTimeInterval)sec;
//变换到指定的透明度opacity
+ (SCNAction *)fadeOpacityTo:(CGFloat)opacity duration:(NSTimeInterval)sec;

    [self.earthNode runAction:[SCNAction fadeOutWithDuration:4]];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.earthNode runAction:[SCNAction fadeInWithDuration:2]];
    });
  • 节点的可视性
//隐藏node
+ (SCNAction *)hide API_AVAILABLE(macos(10.11), ios(9.0));
//不隐藏node
+ (SCNAction *)unhide API_AVAILABLE(macos(10.11), ios(9.0));
  • 移除节点 
//移除节点
+ (SCNAction *)removeFromParentNode;
  • 添加音频播放
//播放音频。 waitForCompletion:YES Action的duration就是音频的时长;NO duration为0。
+ (SCNAction *)playAudioSource:(SCNAudioSource *)source waitForCompletion:(BOOL)wait API_AVAILABLE(macos(10.11), ios(9.0));
  • 创建节点的动画组或者重复节点的动画
//所有动画串行运行
+ (SCNAction *)sequence:(NSArray<SCNAction *> *)actions;
//所有动画并行运行
+ (SCNAction *)group:(NSArray<SCNAction *> *)actions;

//重复action动画,count次
+ (SCNAction *)repeatAction:(SCNAction *)action count:(NSUInteger)count;
//一直重复action动画
+ (SCNAction *)repeatActionForever:(SCNAction *)action;

    [self.earthNode runAction:[SCNAction group:@[[SCNAction rotateToX:5 y:5 z:0 duration:2],[SCNAction fadeOutWithDuration:2]]]];

    [self.earthNode runAction:[SCNAction sequence:@[[SCNAction rotateToX:5 y:5 z:0 duration:2],[SCNAction fadeOutWithDuration:2]]]];
  • 动画延迟设置(适用于//所有动画串行运行+ (SCNAction *)sequence:(NSArray<SCNAction *> *)actions;)
//动画延迟时间
+ (SCNAction *)waitForDuration:(NSTimeInterval)sec;
//动画延迟时间 随机数范围【sec,durationRange/2]
+ (SCNAction *)waitForDuration:(NSTimeInterval)sec withRange:(NSTimeInterval)durationRange;

    [self.earthNode runAction:[SCNAction sequence:@[[SCNAction rotateToX:5 y:5 z:0 duration:2],[SCNAction waitForDuration:2 withRange:6],[SCNAction fadeOutWithDuration:2]]]];
  • 节点自定义动画
//执行动画开始时立即调用,node是正在动画的节点
+ (SCNAction *)runBlock:(void (^)(SCNNode *node))block;
+ (SCNAction *)runBlock:(void (^)(SCNNode *node))block queue:(dispatch_queue_t)queue;

/**
 反复运行脚本,直到动作的持续时间结束。每次SceneKit运行脚本时,它计算自操作开始执行以来的运行时间(作为操作在0.0到1.0之间的时间的一部分),并使脚本作为一个名为elapsedTime的变量可用。

 @param script 包含JavaScript源代码的字符串。
 @param seconds 动作的持续时间,以秒为单位。
 */
+ (SCNAction *)javaScriptActionWithScript:(NSString *)script duration:(NSTimeInterval)seconds;

/**
 自定义动作执行时反复调用

 @param seconds 动作时间
 @param block 节点node,和动作开始经过的时间elapsedTime
 */
+ (SCNAction *)customActionWithDuration:(NSTimeInterval)seconds actionBlock:(void (^)(SCNNode *node, CGFloat elapsedTime))block;
  • 节点动画逆转
- (SCNAction *)reversedAction;
  • 基础属性
//动画时间
@property(nonatomic) NSTimeInterval duration;
//定时模式
@property(nonatomic) SCNActionTimingMode timingMode;
typedef NS_ENUM(NSInteger, SCNActionTimingMode) {
    SCNActionTimingModeLinear,//匀速
    SCNActionTimingModeEaseIn,//先慢后快
    SCNActionTimingModeEaseOut,//先快后慢
    SCNActionTimingModeEaseInEaseOut//先慢中快后慢
} API_AVAILABLE(macos(10.10), ios(8.0));
//使用这个输入来计算定制的计时功能,它的输出决定了动画的计时
@property(nonatomic, nullable) SCNActionTimingFunction timingFunction;
//自定义计时功能。输入时间在0.0到1.0之间。在行动期间。返回值必须为0-1.0并增加。当输入时间达到1.0时,函数必须返回1.0。
typedef float (^SCNActionTimingFunction)(float time);
action.timingMode = SCNActionTimingModeLinear;
action.timingFunction = ^float(float time) {
    return 1.0 - time*time;
};
//速度系数
@property(nonatomic) CGFloat speed;
  • SCNActionable协议
//运行动画
- (void)runAction:(SCNAction *)action API_AVAILABLE(macos(10.10));
//运行动画,动画结束block块
- (void)runAction:(SCNAction *)action completionHandler:(nullable void (^)(void))block API_AVAILABLE(macos(10.10));
//运行动画设置key值
- (void)runAction:(SCNAction *)action forKey:(nullable NSString *)key API_AVAILABLE(macos(10.10));
//运行动画设置key值,动画结束block块
- (void)runAction:(SCNAction *)action forKey:(nullable NSString *)key completionHandler:(nullable void (^)(void))block API_AVAILABLE(macos(10.10));

//是否正在动画
@property(nonatomic, readonly) BOOL hasActions API_AVAILABLE(macos(10.10));
//通过key值获取动画
- (nullable SCNAction *)actionForKey:(NSString *)key API_AVAILABLE(macos(10.10));
//通过key值移除动画
- (void)removeActionForKey:(NSString *)key API_AVAILABLE(macos(10.10));
//移除所有动画
- (void)removeAllActions API_AVAILABLE(macos(10.10));
//读取所有动画key值
@property(nonatomic, readonly) NSArray<NSString *> *actionKeys API_AVAILABLE(macos(10.10));

 

posted @ 2018-05-17 16:23  ForeverGuard  阅读(1090)  评论(0编辑  收藏  举报