ios 3D引擎 SceneKit 开发(6) --SCNAction

本文介绍了在iOS的SceneKit中如何使用SCNAction更简便地创建旋转动画。通过示例代码展示了如何让一个SCNNode沿着特定路径旋转,并详细讲解了SCNAction的主要API,包括移动和旋转等操作。最后,提供了一个GitHub链接,供读者下载完整示例代码进行学习。
摘要由CSDN通过智能技术生成

前面关于旋转的两篇我们利用CABasicAnimation来实现旋转动画,其实在SceneKit中,有一种更为简单的方法去实现一些基础动画,那就是SCNAction,它的执行对象是SCNNode。

一个简单的例子:

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

[shipRotationNode runAction:shipMoveAction];

上面代码很容易理解shipRotationNode 动画移动到(10,10,5)这个位置,时间间隔为4s。

我们下面简单介绍一下 SCNAction 主要的API:


+ (SCNAction *)moveByX:(CGFloat)deltaX
                     y:(CGFloat)deltaY
                     z:(CGFloat)deltaZ
              duration:(NSTimeInterval)duration  

 //将node从x,y,z上各移动多少距离


+ (SCNAction *)moveBy:(SCNVector3)delta
             duration:(NSTimeInterval)duration    

 //同上,只不过传入参数为SCNVector3


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

//将node移动到location这个位置


+ (SCNAction *)rotateByX:(CGFloat)xAngle
                       y:(CGFloat)yAngle
                       z:(CGFloat)zAngle
                duration:(NSTimeInterval)duration

//将node从x,y,z上各旋转多少度


+ (SCNAction *)rotateToX:(CGFloat)xAngle
                       y:(CGFloat)yAngle
                       z:(CGFloat)zAngle
                duration:(
import UIKit import QuartzCore import SceneKit class GameViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // create a new scene let scene = SCNScene(named: "art.scnassets/ship.scn")! // create and add a camera to the scene let cameraNode = SCNNode() cameraNode.camera = SCNCamera() scene.rootNode.addChildNode(cameraNode) // place the camera cameraNode.position = SCNVector3(x: 0, y: 0, z: 15) // create and add a light to the scene let lightNode = SCNNode() lightNode.light = SCNLight() lightNode.light!.type = SCNLightTypeOmni lightNode.position = SCNVector3(x: 0, y: 10, z: 10) scene.rootNode.addChildNode(lightNode) // create and add an ambient light to the scene let ambientLightNode = SCNNode() ambientLightNode.light = SCNLight() ambientLightNode.light!.type = SCNLightTypeAmbient ambientLightNode.light!.color = UIColor.darkGrayColor() scene.rootNode.addChildNode(ambientLightNode) // retrieve the ship node let ship = scene.rootNode.childNodeWithName("ship", recursively: true)! // animate the 3d object ship.runAction(SCNAction.repeatActionForever(SCNAction.rotateByX(0, y: 2, z: 0, duration: 1))) // retrieve the SCNView let scnView = self.view as! SCNView // set the scene to the view scnView.scene = scene // allows the user to manipulate the camera scnView.allowsCameraControl = true // show statistics such as fps and timing information scnView.showsStatistics = true // configure the view scnView.backgroundColor = UIColor.blackColor() // add a tap gesture recognizer let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:") scnView.addGestureRecognizer(tapGesture) } func handleTap(gestureRecognize: UIGestureRecognizer) { // retrieve the SCNView let scnView = self.view as! SCNView // check what nodes are tapped let p = gestureRecognize.locationInView(scnView) let hitResults = scnView.hitTest(p, options: nil) // check that we clicked on at least one object if hitResults.count > 0 { // retrieved the first clicked object let result: AnyObject! = hitResults[0] // get its material let material = result.node!.geometry!.firstMaterial! // highlight it SCNTransaction.begin() SCNTransaction.setAnimationDuration(0.5) // on completion - unhighlight SCNTransaction.setCompletionBlock { SCNTransaction.begin() SCNTransaction.setAnimationDuration(0.5) material.emission.contents = UIColor.blackColor() SCNTransaction.commit() } material.emission.contents = UIColor.redColor() SCNTransaction.commit() } } override func shouldAutorotate() -> Bool { return true } override func prefersStatusBarHidden() -> Bool { return true } override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { if UIDevice.currentDevice().userInterfaceIdiom == .Phone { return .AllButUpsideDown } else { return .All } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Release any cached data, images, etc that aren't in use. } }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值