CoreMotion startDeviceMotionUpdates函数只被调用一次

时间:2017-12-23 16:36:37

标签: ios swift scenekit core-motion

我的viewDidLoad中有以下代码,用于根据设备的方向旋转SCNScene节点“statue”。

当我在设备上运行应用程序时,雕像的初始欧拉角似乎是由设备方向设置的,但移动设备根本不会更新节点的欧拉角,并且节点完全静止。

知道为什么startDeviceMotionUpdates没有连续调用updateStatueOrientation函数?

    // create a new scene
    let scene = SCNScene(named: "art.scnassets/bathroom.scn")!
    //attach statue node to code
    let statue = scene.rootNode.childNode(withName: "statue", recursively: true)!

    let motionManager = CMMotionManager()
    motionManager.showsDeviceMovementDisplay = true
    motionManager.deviceMotionUpdateInterval = 1.0 / 60.0
    if motionManager.isDeviceMotionAvailable {
        print("Motion active")
        motionManager.startDeviceMotionUpdates(to: OperationQueue(), withHandler: { (motion: CMDeviceMotion?, err: Error?) in
            updateStatueOrientation(m:motion!)
            })
    }

    func updateStatueOrientation(m:CMDeviceMotion) {
        statue.eulerAngles.z = Float(m.attitude.yaw  - Double.pi)
        statue.eulerAngles.y = Float(m.attitude.pitch)
        statue.eulerAngles.z = Float(m.attitude.roll)
        print("\(m.attitude.yaw) \(m.attitude.pitch) \(m.attitude.roll)")
    }

0 个答案:

没有答案