在scenekit(swift)中跟踪动画

在scenekit(swift)中跟踪动画,swift,scenekit,Swift,Scenekit,只要我触摸屏幕的右侧,我就尝试在右侧旋转几何体,触摸左侧时,我尝试在左侧旋转几何体。不触摸屏幕时,动画应停止。 为此,我想出了以下代码,直到我再次按下屏幕,几何体返回到其原始状态,而不是从上一个位置开始旋转。 我还需要为将来的开发和节点跟踪角度。rotation.w始终返回0 func respondToLPGesture(gestureRecognize: UILongPressGestureRecognizer) { let scnView = self.view as SCNVie

只要我触摸屏幕的右侧,我就尝试在右侧旋转几何体,触摸左侧时,我尝试在左侧旋转几何体。不触摸屏幕时,动画应停止。 为此,我想出了以下代码,直到我再次按下屏幕,几何体返回到其原始状态,而不是从上一个位置开始旋转。 我还需要为将来的开发和节点跟踪角度。rotation.w始终返回0

func respondToLPGesture(gestureRecognize: UILongPressGestureRecognizer)
{
    let scnView = self.view as SCNView
    let spin = CABasicAnimation(keyPath: "rotation")
    spin.duration = 2
    spin.repeatCount = .infinity

    let p = gestureRecognize.locationInView(scnView)
    switch gestureRecognize.state
        {
    case .Began:
        //Determine rotation direction
        if p.x < scnView.frame.width / 2 {
            //spin.fromValue = NSValue(SCNVector4: SCNVector4(x: 0, y: 0, z: 1, w: angle))
            //spin.toValue = NSValue(SCNVector4: SCNVector4(x: 0, y: 0, z: 1, w: angle + 2*Float(M_PI)))
            spin.byValue = NSValue(SCNVector4: SCNVector4(x: 0, y: 0, z: 1, w: 2*Float(M_PI)))
            scnView.scene.rootNode.childNodes[1].addAnimation(spin, forKey: "spin around")
        }
        else {
            spin.byValue = NSValue(SCNVector4: SCNVector4(x: 0, y: 0, z: 1, w: 2*Float(-M_PI)))
            scnView.scene.rootNode.childNodes[1].addAnimation(spin, forKey: "spin around")
        }
    case .Ended:
        println(scnView.scene.rootNode.childNodes[1].rotation.w) // Always returns 0.
        scnView.scene.rootNode.childNodes[1].pauseAnimationForKey("spin around")

    default:
        println("Inside default")
    }
}
func respondtopgesture(手势识别:ui长按手势识别器)
{
让scnView=self.view作为scnView
让旋转=旋转(关键路径:“旋转”)
自旋。持续时间=2
spin.repeatCount=.infinity
设p=GestureRecognition.locationInView(scnView)
切换手势识别.state
{
案例.开始:
//确定旋转方向
如果p.x
观看WWDC 2014“SceneKit的新功能”帮助我找到了答案。 要在Scenekit中跟踪节点位置/旋转,请使用“presentationNode”。 示例如下:

let angle = scnView.scene.rootNode.childNodes[1].presentationNode().rotation.w
使用以下命令将返回动画目标端的值,在我的代码示例中该值始终为0:

let angle = scnView.scene.rootNode.childNodes[1].rotation.w