我正在使用SceneKit,通过滑动手势让用户旋转他们在球体内所看到的内容。这个功能很好用 - 但是我想让球体保持稍微朝着滑动手势的方向旋转,以获得更好的反应感。
以下是我的场景设置:
这里是当前处理平移手势的方式(由SCNCamera limit arcball rotation提供):
以下是我的场景设置:
// 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
被重置了。如果您对此有任何想法,我全听着。 - H KrunAction
添加完成处理程序,当动作完成时更新lastRatio值? - Andy HeardlastWidthRatio += (((-velocity.x * 0.001)*2) / sender.view.frame.size.width);
- Kevin Goedecke