美文网首页iOS 知识收集
iOS CoreMotion 的一些坑

iOS CoreMotion 的一些坑

作者: angry_zxy | 来源:发表于2018-09-29 18:04 被阅读0次

    CoreMotion

    一般通过CMMotionManager获取传感器数据,出于性能的考虑,在app里面只创建一个CMMotionManger对象,CMMotionManger对象可以获取以下数据:

    • 加速度(原生数据)
    • 陀螺仪(原生数据)
    • 磁力计(原生数据)
    • deviceMotion

    其中deviceMotion里包含了加速度,陀螺仪,磁力计以及欧拉角等信息的对象,一般建议在deviceMotion里取数据,deviceMotion里的数据是将原生数据经过模糊算法处理后的更加准确的值。如果需要获取某个传感器的数据,使用相关的update方法即可。


    数据读取

    传感器数据的获取分为pull和push两种方式,pull是一种主动获取的方式,在需要读取的时候,直接读取CMMotionManger里的数据即可。push是一种被动获取的方式,传感器数据会在一个队列里定时输出。

    注意:pull方式的性能优于push方式,尽量使用pull方式


    关于欧拉角

    yaw: 手机绕Z轴旋转的角度。手机的顶部面向正北方,角度是0,顶部面向正东方,角度是-Pi / 2, 正南是Pi,正西是Pi /2. 整个范围是-Pi到Pi之间。

    pitch:水平面与手机屏幕的夹角。手机与水平面平行放置,角度为0,将手机顶部慢慢抬起远离地面,角度变为正值,反方向为负值。范围是-Pi到Pi之间。

    roll:手机屏幕的垂直平面与水平面的垂直平面的夹角,手机与水平面平行放置,角度为0,将手机左边抬起,远离地面,角度变为正值,反方向为负值,角度的范围是-Pi到Pi

    安卓设备的区别:

    • Azimuth (degrees of rotation about the -z axis). This is the angle between the device's current compass direction and magnetic north. If the top edge of the device faces magnetic north, the azimuth is 0 degrees; if the top edge faces south, the azimuth is 180 degrees. Similarly, if the top edge faces east, the azimuth is 90 degrees, and if the top edge faces west, the azimuth is 270 degrees.
    • Pitch (degrees of rotation about the x axis). This is the angle between a plane parallel to the device's screen and a plane parallel to the ground. If you hold the device parallel to the ground with the bottom edge closest to you and tilt the top edge of the device toward the ground, the pitch angle becomes positive. Tilting in the opposite direction— moving the top edge of the device away from the ground—causes the pitch angle to become negative. The range of values is -180 degrees to 180 degrees.
    • Roll (degrees of rotation about the y axis). This is the angle between a plane perpendicular to the device's screen and a plane perpendicular to the ground. If you hold the device parallel to the ground with the bottom edge closest to you and tilt the left edge of the device toward the ground, the roll angle becomes positive. Tilting in the opposite direction—moving the right edge of the device toward the ground— causes the roll angle to become negative. The range of values is -90 degrees to 90 degrees.

    其实主要是坐标系参考点,以及正方向的定义不同而已,参数表示的意义是一样的。


    deviceMotion为nil的问题

    After some roaming about, we discovered that this was due to a infrequent hardware/firmware error. The wisest thing was to check for both motionManager.deviceMotion being Nil, and the motionManager.deviceMotionActive being true.

    说是罕见的硬件错误,理论确实有可能,不过目前尚未碰到这样的机器,持保留态度

    • 插件(键盘)未开启完全访问

    在ios8系统上,是否开启完全访问,都可以读取到deviceMotion数据。在ios11系统上,不开启完全访问deviceMotion为nil更多情况还待验证

    相关文章

      网友评论

        本文标题:iOS CoreMotion 的一些坑

        本文链接:https://www.haomeiwen.com/subject/btbovftx.html