cameranode rotate as iOS device moving

廉价感情. 提交于 2019-11-27 20:14:28

Device motion is best, as it ties in multiple sensors and fuses the data together in a meaningful way. It's also best to avoid using euler angles, as they suffer from what's known as gimbal lock. Instead, use quaternions and set your camera orientation with them.

I've created a Swift class extension for CMDeviceMotion that'll let you determine what the device is looking at (for any orientation of the screen).

From there, it's straightforward to rotate your camera. First, setup your device motion (in view did appear, or some other appropriate place):

if motionManager.deviceMotionAvailable {

    motionManager.deviceMotionUpdateInterval = 0.017
    motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue(), withHandler: deviceDidMove)
}

Then handle those updates in your deviceDidMove implementation, using the previously mentioned CMDeviceMotion extension:

func deviceDidMove(motion: CMDeviceMotion?, error: NSError?) {

    if let motion = motion {

        yourCameraNode.orientation = motion.gaze(atOrientation: UIApplication.sharedApplication().statusBarOrientation)
    }
}

And now your camera should follow your devices orientation in space.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!