使用panGestureRecognizer

时间:2017-10-24 10:41:03

标签: ios swift constraints uigesturerecognizer scenekit

我是一名新程序员,正在研究一个简单的演示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

1 个答案:

答案 0 :(得分:0)

我通过使用球面坐标系解决了这个问题。

一般的想法是获取起始位置和角度,然后将其转换为球面坐标系。然后你只需改变两个角度即可进行旋转。

使用球面坐标系统来处理捏合手势更加容易。你只需要改变半径。

这是我使用的一些代码。

func handlePan(_ gestureRecognize: UIPanGestureRecognizer) {
    // retrieve scene
    let carView = self.view as! SCNView

    // save node data and pan gesture data
    let cameraNode = carView.scene?.rootNode.childNode(withName: "Camera", recursively: true)!
    //let translation = gestureRecognize.translation(in: gestureRecognize.view!)

    let speed = gestureRecognize.velocity(in: gestureRecognize.view!)
    var speedX = sign(Float(speed.x)) * Float(sqrt(abs(speed.x)))
    var speedY = sign(Float(speed.y)) * Float(sqrt(abs(speed.y)))
    if speedX.isNaN {speedX = 0}
    if speedY.isNaN {speedY = 0}

    // record start value
    let cameraXStart = cameraNode!.position.x
    let cameraYStart = cameraNode!.position.y
    let cameraZStart = cameraNode!.position.z - 1.0

    let cameraAngleStartZ = cameraNode!.eulerAngles.z
    let cameraAngleStartX = cameraNode!.eulerAngles.x
    let radiusSquare = cameraXStart * cameraXStart + cameraYStart * cameraYStart + cameraZStart * cameraZStart

    // calculate delta value
    let deltaAngleZ = -0.003 * Float(speedX)
    let deltaAngleX = -0.003 * Float(speedY)

    // get new Value
    var cameraNewAngleZ = cameraAngleStartZ + deltaAngleZ
    var cameraNewAngleX = cameraAngleStartX + deltaAngleX

    if cameraNewAngleZ >= 100 * Float.pi {
        cameraNewAngleZ = cameraNewAngleZ - 100 * Float.pi
    } else if cameraNewAngleZ < -100 * Float.pi {
        cameraNewAngleZ = cameraNewAngleZ + 100 * Float.pi
    } else {
        // set limit
        if cameraNewAngleX > 1.4 {
            cameraNewAngleX = 1.4
        } else if cameraNewAngleX < 0.1 {
            cameraNewAngleX = 0.1
        }
        // use angle value to get position value
        let cameraNewX =  sqrt(radiusSquare) * cos(cameraNewAngleZ - Float.pi/2) * cos(cameraNewAngleX - Float.pi/2)
        let cameraNewY =  sqrt(radiusSquare) * sin(cameraNewAngleZ - Float.pi/2) * cos(cameraNewAngleX - Float.pi/2)
        let cameraNewZ = -sqrt(radiusSquare) * sin(cameraNewAngleX - Float.pi/2) + 1

        if cameraNode?.camera?.usesOrthographicProjection == false {
            cameraNode?.position = SCNVector3Make(cameraNewX, cameraNewY, cameraNewZ)
            cameraNode?.eulerAngles = SCNVector3Make(cameraNewAngleX, 0, cameraNewAngleZ)
        }
        else if cameraNode?.camera?.usesOrthographicProjection == true {
            cameraNode?.position = SCNVector3Make(0, 0, 10)
            cameraNode?.eulerAngles = SCNVector3Make(0, 0, cameraNewAngleZ)
        }
    }
}