iOS 模拟轮胎滚动3D动画效果

因为项目中需要实现跑车动画效果,于是希望采用静止的跑车图和转动的轮胎来实现跑车开动效果。首先轮胎的转动要考虑到3D环境中形状的变化,不应该是纯圆形的,而应该是椭圆形。有两种方法实现这样的效果,

一是通过连续的贴图不断的替换来得到需要的滚动的动画,优点是实现简单,不断循环替换图片就能得到滚动的效果,更加贴近真实的样子,有更多的细节。但是遇到线程繁忙时将会影响动画的效果,拉长动画的持续时间;同时增加了设计人员的压力和软件大小。

二是通过一张纯圆的轮胎贴图,通过代码来y轴旋转得到椭圆的接近真实环境下的轮胎形状,再通过z轴的转动来实现动画效果。这样的实现难度更高(计算rotation的值比较复杂),模拟的轮胎没有真实的那个角度的轮胎细节多。但是在系统卡的时候,会自动减少动画的帧数,而基本保证动画的持续时间,减少程序负荷。当然也更加节省设计的工作量。

综合考虑还是采用后者,毕竟轮胎滚动的时候速度快,显示的细节不容易观察到。加上礼物大量发送时可能造成堵塞,所以考虑选择后者来保证流畅度。

首先,让图片围绕y轴转动50度来模拟斜前方看到轮胎的椭圆效果。代码如下:

    [imageView.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
    [imageView.layer setTransform: CATransform3DMakeRotation(50 * (M_PI / 180.0f), 0, 1, 0)];
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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. } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值