我正在使用SceneKit並允許用戶使用平移手勢旋轉他們在球體內看到的內容。這工作正常 - 除了我想讓球體在平移手勢的方向上稍微旋轉一點,感覺更加反應一點。SceneKit - 爲通過平移手勢旋轉的球體添加漂移
這是我該場景的設置:
// Create scene
let scene = SCNScene()
sceneView.scene = scene
let panRecognizer = UIPanGestureRecognizer(target: self,
action: #selector(ViewController.handlePanGesture(_:)))
sceneView.addGestureRecognizer(panRecognizer)
//Create sphere
let sphere = SCNSphere(radius: 50.0)
// sphere setup
sphereNode = SCNNode(geometry: sphere)
sphereNode!.position = SCNVector3Make(0,0,0)
scene.rootNode.addChildNode(sphereNode!)
// Create camera
let camera = SCNCamera()
// camera setup
cameraNode = SCNNode()
cameraNode!.camera = camera
cameraNode!.position = SCNVector3Make(0, 0, 0)
cameraOrbit = SCNNode()
cameraOrbit!.addChildNode(cameraNode!)
scene.rootNode.addChildNode(cameraOrbit!)
let lookAtConstraint = SCNLookAtConstraint(target: sphereNode!)
lookAtConstraint.gimbalLockEnabled = true
cameraNode!.constraints = [lookAtConstraint]
sceneView.pointOfView = cameraNode
這裏是移動手勢當前如何處理(的SCNCamera limit arcball rotation提供):
let translation = sender.translationInView(sender.view!)
let widthRatio = Float(translation.x)/Float(sender.view!.frame.size.width) + lastWidthRatio
let heightRatio = Float(translation.y)/Float(sender.view!.frame.size.height) + lastHeightRatio
self.cameraOrbit?.eulerAngles.y = Float(-2 * M_PI) * widthRatio
self.cameraOrbit?.eulerAngles.x = Float(-M_PI) * heightRatio
if (sender.state == .Ended) {
lastWidthRatio = widthRatio
lastHeightRatio = heightRatio
}
我不知道在哪裏的開始加入輕微的持續旋轉運動。也許增加一個physicsBody
並施加力量?也許動畫eulerAngles
的變化?
我最終做了類似的事情 - 謝謝!我現在的問題是'lastRatio'值現在已關閉,因爲球體的最終靜止位置比最後一次設置的值旋轉得更遠。因此,當用戶進行下一個平移手勢時,隨着「eulerAngles」被重置,相機「跳」到漂移開始之前的位置。如果你對這個問題有任何想法,我就會全神貫注。 –
嗯,也許嘗試添加一個完成處理程序到'runAction',當動作完成時再次更新lastRatio值? –
@AndyHeard我嘗試過,但它不工作,我真的不明白爲什麼。這裏是我的代碼片段'''lastWidthRatio + =(((--velocity.x * 0.001)* 2)/ sender.view.frame.size.width);''' –