ios 3D引擎 SceneKit 開(kāi)發(fā)(6) --SCNAction

前面關(guān)于旋轉(zhuǎn)的兩篇我們利用CABasicAnimation來(lái)實(shí)現(xiàn)旋轉(zhuǎn)動(dòng)畫,其實(shí)在SceneKit中,有一種更為簡(jiǎn)單的方法去實(shí)現(xiàn)一些基礎(chǔ)動(dòng)畫,那就是SCNAction,它的執(zhí)行對(duì)象是SCNNode。

一個(gè)簡(jiǎn)單的例子:

SCNAction *shipMoveAction = [SCNAction moveTo:SCNVector3Make(10,10,5) duration:4];

[shipRotationNode runAction:shipMoveAction];

上面代碼很容易理解shipRotationNode 動(dòng)畫移動(dòng)到(10,10,5)這個(gè)位置,時(shí)間間隔為4s。

我們下面簡(jiǎn)單介紹一下 SCNAction 主要的API:

+ (SCNAction *)moveByX:(CGFloat)deltaX

?????????????????????????????????????????y:(CGFloat)deltaY?

?????????????????????????????????????????z:(CGFloat)deltaZ?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?duration:(NSTimeInterval)duration?

?//將node從x,y,z上各移動(dòng)多少距離

+ (SCNAction *)moveBy:(SCNVector3)delta?

? ? ? ? ? ? ? ? ? ? ? ? ? ?duration:(NSTimeInterval)duration?

?//同上,只不過(guò)傳入?yún)?shù)為SCNVector3

+ (SCNAction *)moveTo:(SCNVector3)location?

? ? ? ? ? ? ? ? ? ? ? ? ? duration:(NSTimeInterval)duration

//將node移動(dòng)到location這個(gè)位置

+ (SCNAction *)rotateByX:(CGFloat)xAngle?

?????????????????????????????????????????y:(CGFloat)yAngle?

?????????????????????????????????????????z:(CGFloat)zAngle?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?duration:(NSTimeInterval)duration

//將node從x,y,z上各旋轉(zhuǎn)多少度

+ (SCNAction *)rotateToX:(CGFloat)xAngle?

?????????????????????????????????????????y:(CGFloat)yAngle?

?????????????????????????????????????????z:(CGFloat)zAngle?

?????????????????????????????duration:(NSTimeInterval)duration

/將node從x,y,z上旋轉(zhuǎn)到指定角度

+ (SCNAction *)rotateToX:(CGFloat)xAngle?

?????????????????????????????????????????y:(CGFloat)yAngle?

?????????????????????????????????????????z:(CGFloat)zAngle?

?????????????????????????????duration:(NSTimeInterval)duration?

?????????????????shortestUnitArc:(BOOL)shortestUnitArc

// 同上,與上面的方法區(qū)別在于多了shortestUnitArc 這個(gè)參數(shù),BOOL值。

//舉個(gè)例子:我們需要將一個(gè)node從 0度旋轉(zhuǎn)到270度,

//如果將shortestUnitArc設(shè)置為NO,node會(huì)順時(shí)針旋轉(zhuǎn)到270度;

//如果將shortestUnitArc設(shè)置為YES,node會(huì)逆時(shí)針旋轉(zhuǎn)90度到270度,

//即選擇最小的旋轉(zhuǎn)角度旋轉(zhuǎn)到特定的度數(shù)。默認(rèn)為NO。

+ (SCNAction *)rotateByAngle:(CGFloat)angle?

?????????????????????????????????aroundAxis:(SCNVector3)axis?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? duration:(NSTimeInterval)duration

// 沿著特定的軸旋轉(zhuǎn)angle度。前面旋轉(zhuǎn)都是沿x,y,z軸旋轉(zhuǎn),都是互相垂直的,

//大家有沒(méi)有想過(guò)如何沿著與x軸成45度夾角的方向旋轉(zhuǎn)node??

//這個(gè)API大家這里留意一下,

//上篇提到的不在X-Z 這個(gè)平面旋轉(zhuǎn),會(huì)用這個(gè)方法在后面的demo中解決。

+ (SCNAction *)rotateToAxisAngle:(SCNVector4)axisAngle?

?????????????????????????????????????????????duration:(NSTimeInterval)duration

// SCNVector4(x,y,z,angle) 沿著特定的軸旋轉(zhuǎn)到angle度。

//這里解釋一下angle 類似π,如果angle=2,

//我們可不能理解為旋轉(zhuǎn)到2度,而是旋轉(zhuǎn)到2/π*180 度。

+ (SCNAction *)scaleBy:(CGFloat)scale?

?????????????????????????duration:(NSTimeInterval)sec

//縮?。ǚ糯螅┒嗌?/p>

+ (SCNAction *)scaleTo:(CGFloat)scale?

?????????????????????????duration:(NSTimeInterval)sec

//縮?。ǚ糯螅┑蕉嗌?/p>

+ (SCNAction *)fadeInWithDuration:(NSTimeInterval)sec

//字面意思可以理解,淡入。將node 的opacity 漸漸變成1

+ (SCNAction *)fadeOutWithDuration:(NSTimeInterval)sec

// 淡出

+ (SCNAction *)fadeOpacityBy:(CGFloat)factor?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? duration:(NSTimeInterval)sec

//將node 的opacity 漸漸變化特定的數(shù)值

+ (SCNAction *)fadeOpacityTo:(CGFloat)opacity?

?????????????????????????????????????duration:(NSTimeInterval)sec

//將node 的opacity 漸漸變化到特定的數(shù)值

+ (SCNAction *)hide

// 隱藏node

+ (SCNAction *)unhide

//顯示node

+ (SCNAction *)removeFromParentNode

//移除node

+ (SCNAction *)playAudioSource:(SCNAudioSource *)source?

? ? ? ? ? ? ? ? ? ? ? ? ?waitForCompletion:(BOOL)wait

//播放音頻。 waitForCompletion,BOOL值,

//如果為YES Action的duration就是音頻的時(shí)長(zhǎng);

//如果為NO,可以認(rèn)為duration 為0。?

//可以去看SCNAudioPlayer 的API.

+ (SCNAction *)group:(NSArray*)actions

//group 被用來(lái)并發(fā)執(zhí)行多個(gè)SCNAction

+ (SCNAction *)sequence:(NSArray*)actions

//順序執(zhí)行多個(gè)SCNAction,上個(gè)SCNAction執(zhí)行結(jié)束后,才執(zhí)行下個(gè)SCNAction

+ (SCNAction *)repeatAction:(SCNAction *)action

????????????????????????????????????????count:(NSUInteger)count

//將一個(gè)SCNAction執(zhí)行count 次

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

// 一直執(zhí)行某個(gè)SCNAction

+ (SCNAction *)waitForDuration:(NSTimeInterval)sec

//延遲SCNAction,比如用sequence 順序執(zhí)行多個(gè)SCNAction時(shí),

//可以給SCNAction a,c? 中間添加一個(gè)SCNAction b,

//等a執(zhí)行結(jié)束后,延遲一會(huì),再去執(zhí)行c

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

//自定義SCNAction ,你可以在block 做一些操作

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

????????????????????????????????queue:(dispatch_queue_t)queue

//在一個(gè)特定的隊(duì)列中,執(zhí)行block

+ (SCNAction *)customActionWithDuration:(NSTimeInterval)seconds

????????????????????????????????actionBlock:(void (^)(SCNNode *node, CGFloat elapsedTime))block

//上篇數(shù)學(xué)旋轉(zhuǎn)用到的方法,當(dāng)這個(gè)SCNAction執(zhí)行時(shí),

//SceneKit 在這個(gè)時(shí)間間隔內(nèi)會(huì)重復(fù)調(diào)用actionBlock,

//并將已逝去的時(shí)間傳給actionBlock

+ (SCNAction *)javaScriptActionWithScript:(NSString *)script

????????????????????????????????????????????????????????????duration:(NSTimeInterval)seconds

//在時(shí)間間隔內(nèi),執(zhí)行一段JavaScript代碼

- (SCNAction *)reversedAction

//逆轉(zhuǎn)一個(gè)已經(jīng)創(chuàng)建的SCNAction,很好理解,

//相當(dāng)于CABasicAnimation的autoreverses屬性。哪里來(lái)的,回哪里去。

@property(nonatomic) NSTimeInterval duration

//SCNAction 的屬性,時(shí)間間隔,真實(shí)時(shí)間間隔受speed影響

@property(nonatomic) CGFloat speed

//SCNAction 的屬性,速度系數(shù)。假設(shè)duration 為10,但speed 為2的話,

//就是速度是以前的2倍,實(shí)際duration 就為5

@property(nonatomic) SCNActionTimingMode timingMode

//SCNAction 的屬性,定時(shí)模式,有四個(gè)常量值:

Constants

SCNActionTimingModeLinear

//勻速

SCNActionTimingModeEaseIn

//一開(kāi)始慢,慢慢加快

SCNActionTimingModeEaseOut

//一開(kāi)始快,逐漸變慢

SCNActionTimingModeEaseInEaseOut

//開(kāi)始慢慢地,通過(guò)中間的時(shí)候加速,然后再次放緩

OK,了解過(guò)SCNAction的API后,我們就可以解決上一篇的問(wèn)題了:添加一艘飛船,讓它繞著與x軸成45度的方向做圓周運(yùn)動(dòng)。

用到的API :

+ (SCNAction *)rotateByAngle:(CGFloat)angle

????????????????????????????????aroundAxis:(SCNVector3)axis

????????????????????????????????????duration:(NSTimeInterval)duration


最終實(shí)現(xiàn)效果:

代碼全在demo里面,感興趣的同學(xué)可以下載

https://github.com/pzhtpf/SceneKitRoationDemo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容