Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 核心动议:如何分辨哪条路是“路”;向上;?_Ios_Core Motion - Fatal编程技术网

Ios 核心动议:如何分辨哪条路是“路”;向上;?

Ios 核心动议:如何分辨哪条路是“路”;向上;?,ios,core-motion,Ios,Core Motion,我正试图复制Compass应用程序中的功能,但我陷入了一个特殊的问题:如何确定界面中哪条路是“向上”的 我在屏幕上有一个标签,我有下面的代码,可以在设备移动时使其保持水平: self.motionManager = CMMotionManager() self.motionManager?.gyroUpdateInterval = 1/100 self.motionManager?.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueu

我正试图复制Compass应用程序中的功能,但我陷入了一个特殊的问题:如何确定界面中哪条路是“向上”的

我在屏幕上有一个标签,我有下面的代码,可以在设备移动时使其保持水平:

self.motionManager = CMMotionManager()
self.motionManager?.gyroUpdateInterval = 1/100
self.motionManager?.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { (deviceMotion, error) -> Void in
  let roll = -deviceMotion.attitude.roll
  self.tiltLabel?.transform = CGAffineTransformRotate(CGAffineTransformIdentity, CGFloat(roll))
})
这种效果非常好,但也有一些状态是错误的——例如,当iPhone的lightning接口指向上时,标签会不稳定地翻转

如何使用CoreMotion一致地判断哪个方向向上?


更新:显然,横滚/俯仰/偏航都会受到影响-因此我认为正确的解决方案可能涉及使用,但不会受到此问题的影响,或者CMAttitude上的旋转矩阵可能会有所帮助:

对于2D情况,不需要太复杂。“向上”表示“重力相反”,因此:

但如果你想在所有3维空间中都这么做,那么简单的“反向重力”就没有什么意义了:重力的方向不会告诉你手机在重力矢量周围的角度(如果你的手机面朝上,这就是偏航角)。为了进行三维校正,我们可以使用横摇、俯仰和偏航测量:

// Add some perspective so the label looks (roughly) the same,
// no matter what angle the device is held at.
var t = self.view.layer.sublayerTransform
t.m34 = 1/300
self.view.layer.sublayerTransform = t

motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) { (motion, error) in
    let a = motion.attitude
    self.tiltLabel.layer.transform =
        CATransform3DRotate(
            CATransform3DRotate(
                CATransform3DRotate(
                    CATransform3DMakeRotation(CGFloat(a.roll), 0, -1, 0),
                    CGFloat(a.pitch), 1, 0, 0),
                CGFloat(a.yaw), 0, 0, 1),
            CGFloat(-M_PI_2), 1, 0, 0) // Extra pitch to make the label point "up" away from gravity
}

我知道这是一个迟来的回应-但谢谢你。几个月来,我断断续续地盯着四元数维基百科页面:)
// Add some perspective so the label looks (roughly) the same,
// no matter what angle the device is held at.
var t = self.view.layer.sublayerTransform
t.m34 = 1/300
self.view.layer.sublayerTransform = t

motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) { (motion, error) in
    let a = motion.attitude
    self.tiltLabel.layer.transform =
        CATransform3DRotate(
            CATransform3DRotate(
                CATransform3DRotate(
                    CATransform3DMakeRotation(CGFloat(a.roll), 0, -1, 0),
                    CGFloat(a.pitch), 1, 0, 0),
                CGFloat(a.yaw), 0, 0, 1),
            CGFloat(-M_PI_2), 1, 0, 0) // Extra pitch to make the label point "up" away from gravity
}