How to rotate object in a scene with pan gesture - SceneKit(如何使用平移手势旋转场景中的对象 - SceneKit)
本文介绍了如何使用平移手势旋转场景中的对象 - SceneKit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用 Scene 工具包创建一个应用程序来解决 Rubix Cube.我有自己的多维数据集 dae 文件.这是我在 viewDidLoad 中的设置代码
I'm tying to create an app with Scene kit to solve Rubix Cube. I've my own dae file for the cube. this is my setup code in viewDidLoad
let myscene = SCNScene(named: "Rubik1.scnassets/Rubiks_Cube.dae")
scene.rootNode.addChildNode((myscene?.rootNode)!)
// retrieve the SCNView
let scnView = self.view as! SCNView
// set the scene to the view
scnView.scene = scene
geometryNode = (scnView.scene?.rootNode.childNodeWithName("Cube",recursively: true))!
let panRecognizer = UIPanGestureRecognizer(target: self, action: "panGesture:")
scnView.addGestureRecognizer(panRecognizer)
在识别平移手势以旋转立方体时
upon recognising a pan gesture to rotate the cube
func panGesture(gestureRecognize: UIPanGestureRecognizer){
let translation = gestureRecognize.translationInView(gestureRecognize.view!)
let x = Float(translation.x)
let y = Float(-translation.y)
let anglePan = sqrt(pow(x,2)+pow(y,2))*(Float)(M_PI)/180.0
var rotationVector = SCNVector4()
rotationVector.x = -y
rotationVector.y = x
rotationVector.z = 0
rotationVector.w = anglePan
geometryNode.rotation = rotationVector
//geometryNode.transform = SCNMatrix4MakeRotation(anglePan, -y, x, 0)
if(gestureRecognize.state == UIGestureRecognizerState.Ended) {
//
}
}
上面的代码不保留以前的平移手势.如何使用旋转向量"或
above code doesn't preserve the previous pan gestures. how do I use "rotationvector" or
SCNMatrix4MakeRotation(anglePan, -y, x, 0)
旋转立方体
推荐答案
问题解决了
if(gestureRecognize.state == UIGestureRecognizerState.Ended) {
let currentPivot = geometryNode.pivot
let changePivot = SCNMatrix4Invert( geometryNode.transform)
geometryNode.pivot = SCNMatrix4Mult(changePivot, currentPivot)
geometryNode.transform = SCNMatrix4Identity
}
这篇关于如何使用平移手势旋转场景中的对象 - SceneKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何使用平移手势旋转场景中的对象 - SceneKit
基础教程推荐
猜你喜欢
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
