iOS CoreMotion 的一些坑

CoreMotion

一般通過CMMotionManager獲取傳感器數(shù)據(jù),出于性能的考慮,在app里面只創(chuàng)建一個(gè)CMMotionManger對(duì)象,CMMotionManger對(duì)象可以獲取以下數(shù)據(jù):

  • 加速度(原生數(shù)據(jù))
  • 陀螺儀(原生數(shù)據(jù))
  • 磁力計(jì)(原生數(shù)據(jù))
  • deviceMotion

其中deviceMotion里包含了加速度,陀螺儀,磁力計(jì)以及歐拉角等信息的對(duì)象,一般建議在deviceMotion里取數(shù)據(jù),deviceMotion里的數(shù)據(jù)是將原生數(shù)據(jù)經(jīng)過模糊算法處理后的更加準(zhǔn)確的值。如果需要獲取某個(gè)傳感器的數(shù)據(jù),使用相關(guān)的update方法即可。


數(shù)據(jù)讀取

傳感器數(shù)據(jù)的獲取分為pull和push兩種方式,pull是一種主動(dòng)獲取的方式,在需要讀取的時(shí)候,直接讀取CMMotionManger里的數(shù)據(jù)即可。push是一種被動(dòng)獲取的方式,傳感器數(shù)據(jù)會(huì)在一個(gè)隊(duì)列里定時(shí)輸出。

注意:pull方式的性能優(yōu)于push方式,盡量使用pull方式


關(guān)于歐拉角

yaw: 手機(jī)繞Z軸旋轉(zhuǎn)的角度。手機(jī)的頂部面向正北方,角度是0,頂部面向正東方,角度是-Pi / 2, 正南是Pi,正西是Pi /2. 整個(gè)范圍是-Pi到Pi之間。

pitch:水平面與手機(jī)屏幕的夾角。手機(jī)與水平面平行放置,角度為0,將手機(jī)頂部慢慢抬起遠(yuǎn)離地面,角度變?yōu)檎?,反方向?yàn)樨?fù)值。范圍是-Pi到Pi之間。

roll:手機(jī)屏幕的垂直平面與水平面的垂直平面的夾角,手機(jī)與水平面平行放置,角度為0,將手機(jī)左邊抬起,遠(yuǎn)離地面,角度變?yōu)檎?,反方向?yàn)樨?fù)值,角度的范圍是-Pi到Pi

安卓設(shè)備的區(qū)別:

  • 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.

其實(shí)主要是坐標(biāo)系參考點(diǎn),以及正方向的定義不同而已,參數(shù)表示的意義是一樣的。


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.

說是罕見的硬件錯(cuò)誤,理論確實(shí)有可能,不過目前尚未碰到這樣的機(jī)器,持保留態(tài)度

  • 插件(鍵盤)未開啟完全訪問

在ios8系統(tǒng)上,是否開啟完全訪問,都可以讀取到deviceMotion數(shù)據(jù)。在ios11系統(tǒng)上,不開啟完全訪問deviceMotion為nil更多情況還待驗(yàn)證

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容