前面關(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
//縮小(放大)多少
+ (SCNAction *)scaleTo:(CGFloat)scale?
?????????????????????????duration:(NSTimeInterval)sec
//縮衅链酢(放大)到多少
+ (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
//一開始慢,慢慢加快
SCNActionTimingModeEaseOut
//一開始快岩榆,逐漸變慢
SCNActionTimingModeEaseInEaseOut
//開始慢慢地,通過(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