Core Motion可以讓開發(fā)者從各個內(nèi)置傳感器那里獲取未經(jīng)修改的傳感數(shù)據(jù),并觀測或響應設備各種運動和角度變化使碾。通過這些傳感器可以獲取加速度值蜜徽,陀螺儀值,設備motion值等票摇。
CoreMotionManager
CoreMotionManager類能夠使用到設備的所有移動數(shù)據(jù)(motion data)拘鞋,Core Motion框架提供了兩種對motion數(shù)據(jù)的操作方式,一個是"pull"矢门,另一個是"push"盆色,其中"pull"
方式能夠以CoreMotionManager的只讀方式獲取當前任何傳感器狀態(tài)或是組合數(shù)據(jù)灰蛙。"push"方式則是以塊或者閉包的形式收集到你想要得到的數(shù)據(jù)并且會在特定周期內(nèi)得到實時的更新。
push:提供一個線程管理器NSOperationQueue和一個回調(diào)Block隔躲,CoreMotion自動在每一個采樣數(shù)據(jù)到來的時候回調(diào)這個Block摩梧,進行處理。在這種情況下宣旱,Block中的操作會在你自己的主線程內(nèi)執(zhí)行
pull:你必須主動去向CMMotionManager要數(shù)據(jù)仅父,這個數(shù)據(jù)就是最近一次的采樣數(shù)據(jù)。你不去要浑吟,CMMotionManager就不會給你,在這種情況下笙纤,Core Motion所有的操作都在自己的后臺線程中進行,不會有任何干擾你當前線程的行為
Core Motion的基本使用:初始化->獲取數(shù)據(jù)->處理數(shù)據(jù)->不需要時 停止獲取數(shù)據(jù), 為了保證性能组力,蘋果建議在使用CoreMotionManager的時候采用單例模式省容。
-(void)stopAccelerometerUpdates;//停止獲取加速度計數(shù)據(jù)
-(void)stopGyroUpdates;//停止獲取陀螺儀數(shù)據(jù)
-(void)stopDeviceMotionUpdates;//停止獲取設備motion數(shù)據(jù)
CoreMotion主要負責三種數(shù)據(jù):
加速度值CMAccelerometerData
陀螺儀值CMGyroData
設備motion值CMDeviceMotion
CMDeviceMotion屬性介紹:
attitude:通俗來講,就是告訴你手機在當前空間的位置和姿勢
gravity:重力信息忿项,其本質(zhì)是重力加速度矢量在當前設備的參考坐標系中的表達
userAcceleration:加速度信息
rotationRate:即時的旋轉(zhuǎn)速率蓉冈,是陀螺儀的輸出
CMMotionManager *mManager = [[CMMotionManager alloc] init];
_mManager = mManager;
//加速度器檢測
if ([mManager isAccelerometerAvailable]) {//是否可用
NSLog(@"Accelerometer is available");
}else{
NSLog(@"Accelerometer is not available");
}
if ([mManager isAccelerometerActive]) {//是否啟動
NSLog(@"Accelerometer is active");
}else{
NSLog(@"Accelerometer is not actiove");
}
//陀螺儀檢測
if ([mManager isGyroAvailable]) {
NSLog(@"Gryro is available");
}else{
NSLog(@"Gryro is not available");
}
if ([mManager isGyroActive]) {
NSLog(@"Gryo is active");
}else{
NSLog(@"Gryo is not active");
}
/************************獲取加速度***********************/
//1.push 方式
if ([mManager isAccelerometerAvailable] ) {
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
mManager.accelerometerUpdateInterval = 0.01;
[mManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
NSLog(@"X = %.04f, Y = %.04f, Z = %.04f",accelerometerData.acceleration.x, accelerometerData.acceleration.y, accelerometerData.acceleration.z);
}];
}
//2.pull方式 備注:pull方式獲取acceletation數(shù)據(jù),需要自己實現(xiàn)計時器功能
if ([mManager isAccelerometerAvailable]) {//是否可用
NSLog(@"111Accelerometer is available");
mManager.accelerometerUpdateInterval = 0.01;//頻率100HZ
[mManager startAccelerometerUpdates];//開始更新
}
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateAccelerometer) userInfo:nil repeats:YES];
[timer fire];
/************************獲取陀螺儀***********************/
if ([mManager isGyroAvailable]) {
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
mManager.accelerometerUpdateInterval = 0.01;
[mManager startGyroUpdatesToQueue:queue withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
NSLog(@"X = %.04f, Y = %.04f, Z = %.04f",gyroData.rotationRate.x, gyroData.rotationRate.y, gyroData.rotationRate.z);
}];
}
參考:http://www.cocoachina.com/ios/20141103/10111.html