【发布时间】:2018-04-05 02:59:01
【问题描述】:
我是一名新程序员,正在开发一个简单的 iOS 演示程序,我使用 SceneKit 来渲染场景。
我想旋转相机以查看场景的不同视角。但最初的相机控制有点棘手。特别是当我想向一个方向旋转场景时。例如如果我从右向左滑动,相机从右向左移动,然后向下,然后向上,然后再次向下,最后再次向左。
我想要实现的是,当我从右向左滑动时,相机围绕 z 轴旋转。当我上下滑动相机时,只需上下移动。而且我还可以放大和缩小。另外我想设置一个约束,这样相机就不会进入地下。
所以我想出了一个想法,我将相机固定在我关注的对象上。然后将对象放在场景的中心。当我滑动屏幕时,我想围绕 z 轴旋转场景。
我假设我使用 panGestureRecognizer 并将位置更改转换为旋转顺序。但是我该如何实现呢?
或者您有其他想法来获得此结果?
这是我到目前为止所写内容的简单版本。我尝试使用 UIPanGestureRecognizer 但它不起作用,所以我将其删除并设置 allowCameraControl = true
let sceneView = SCNView(frame: self.view.frame)
self.view.addSubview(sceneView)
let scene = SCNScene()
sceneView.scene = scene
let camera = SCNCamera()
let cameraNode = SCNNode()
cameraNode.camera = camera
cameraNode.position = SCNVector3(x: -5.0, y: 5.0, z: 5.0)
let light = SCNLight()
light.type = SCNLight.LightType.spot
light.spotInnerAngle = 30
light.spotOuterAngle = 80
light.castsShadow = true
let lightNode = SCNNode()
lightNode.light = light
lightNode.position = SCNVector3(x: 1.5, y: 1.5, z: 1.5)
let ambientLight = SCNLight()
ambientLight.type = SCNLight.LightType.ambient
ambientLight.color = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
cameraNode.light = ambientLight
let cubeGeometry = SCNBox(width: 1.0, height: 1.0, length: 1.0, chamferRadius: 0.0)
let cubeNode = SCNNode(geometry: cubeGeometry)
let planeGeometry = SCNPlane(width: 50.0, height: 50.0)
let planeNode = SCNNode(geometry: planeGeometry)
planeNode.eulerAngles = SCNVector3(x: GLKMathDegreesToRadians(-90), y: 0, z: 0)
planeNode.position = SCNVector3(x: 0, y: -0.5, z: 0)
cameraNode.position = SCNVector3(x: -3.0, y: 3.0, z: 3.0)
let constraint = SCNLookAtConstraint(target: cubeNode)
constraint.isGimbalLockEnabled = true
cameraNode.constraints = [constraint]
lightNode.constraints = [constraint]
scene.rootNode.addChildNode(lightNode)
scene.rootNode.addChildNode(cameraNode)
scene.rootNode.addChildNode(cubeNode)
scene.rootNode.addChildNode(planeNode)
sceneView.allowsCameraControl = true
【问题讨论】:
标签: ios swift constraints uigesturerecognizer scenekit