CoreMotion.framework框架介紹
CoreMotion.framework
是iOS中的一個核心運動框架,它能夠滿足我們手機許多應(yīng)用的一些需求仑性,比如:
- 指南針
- 加速計:微信搖一搖
- 游戲中根據(jù)重力感應(yīng)的操作
- 計步器:知道我們每天走了多少步
一惶楼、加速計
檢測設(shè)備在X、Y诊杆、Z軸上的加速度 (哪個方向有力的作用歼捐,哪個方向運動了)
根據(jù)加速度數(shù)值,就可以判斷出在各個方向上的作用力度
注:重力不可忽略3啃凇窥岩!
在iOS5之前我們用UIAccelerometer來獲取加速度,用法非常簡單宰缤,不過iOS5之后就過期了颂翼,就用CoreMotion.framework了。
注:必須真機測試慨灭,不適用于模擬器
使用方法
加速計的數(shù)據(jù)獲取方式有兩種:push和pull
push
:
提供一個線程管理器NSOperationQueue和一個回調(diào)Block朦乏,CoreMotion自動在每一個采樣數(shù)據(jù)到來的時候回調(diào)這個Block,進行處理氧骤。在這種情況下呻疹,Block中的操作會在你自己的主線程內(nèi)執(zhí)行。
pull
:
你必須主動去向CMMotionManager要數(shù)據(jù)筹陵,這個數(shù)據(jù)就是最近一次的采樣數(shù)據(jù)刽锤。你不去要,CMMotionManager就不會給你朦佩。
首先引入CoreMotion.framework框架框架
#import <CoreMotion/CoreMotion.h>
創(chuàng)建一個管理類的對象
@property (nonatomic, strong) CMMotionManager *motionManager;
1并思、使用push方式獲取加速計數(shù)據(jù):
- (void)accelerometerPush
{
// 1.初始化運動管理對象
self.motionManager = [[CMMotionManager alloc] init];
// 2.判斷加速計是否可用
if (![self.motionManager isAccelerometerAvailable]) {
NSLog(@"加速計不可用");
return;
}
// 3.設(shè)置加速計更新頻率,以秒為單位
self.motionManager.accelerometerUpdateInterval = 0.1;
// 4.開始實時獲取
[self.motionManager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
//獲取加速度
CMAcceleration acceleration = accelerometerData.acceleration;
NSLog(@"加速度 == x:%f, y:%f, z:%f", acceleration.x, acceleration.y, acceleration.z);
}];
}
2语稠、使用pull方式獲取加速計數(shù)據(jù):
- (void)accelerometerPull
{
// 1.初始化運動管理對象
self.motionManager = [[CMMotionManager alloc] init];
// 2.判斷加速計是否可用
if (![self.motionManager isAccelerometerAvailable]) {
NSLog(@"加速計不可用");
return;
}
// 3.開始更新
[self.motionManager startAccelerometerUpdates];
}
//在需要的時候獲取值
- (void)getAccelerometerData
{
CMAcceleration acceleration = self.motionManager.accelerometerData.acceleration;
NSLog(@"加速度 == x:%f, y:%f, z:%f", acceleration.x, acceleration.y, acceleration.z);
}
二宋彼、陀螺儀
陀螺儀的主要作用弄砍,是基于角動量守恒的原理,沿著某個特定的坐標(biāo)軸測量旋轉(zhuǎn)速率输涕。在使用中音婶,陀螺儀的轉(zhuǎn)子在高速旋轉(zhuǎn)時,始終指向一個固定的方向莱坎,當(dāng)運動物體的運動方向偏離預(yù)定方向時衣式,陀螺儀就可以感受出來。
注:必須真機測試檐什,不適用于模擬器
使用方法
陀螺儀的數(shù)據(jù)獲取方式同樣也有兩種:push和pull碴卧,跟加速計的方式一樣!
首先引入CoreMotion.framework框架框架
#import <CoreMotion/CoreMotion.h>
創(chuàng)建一個管理類的對象
@property (nonatomic, strong) CMMotionManager *motionManager;
1厢汹、使用push方式獲取陀螺儀數(shù)據(jù):
- (void)gyroPush
{
// 1.初始化運動管理對象
self.motionManager = [[CMMotionManager alloc] init];
// 2.判斷陀螺儀是否可用
if (![self.motionManager isGyroAvailable]) {
NSLog(@"陀螺儀不可用");
return;
}
// 3.設(shè)置陀螺儀更新頻率螟深,以秒為單位
self.motionManager.gyroUpdateInterval = 0.1;
// 4.開始實時獲取
[self.motionManager startGyroUpdatesToQueue:[[NSOperationQueue alloc] init] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
//獲取陀螺儀數(shù)據(jù)
CMRotationRate rotationRate = gyroData.rotationRate;
NSLog(@"加速度 == x:%f, y:%f, z:%f", rotationRate.x, rotationRate.y, rotationRate.z);
}];
}
2、使用pull方式獲取陀螺儀數(shù)據(jù):
- (void)gyroPull
{
// 1.初始化運動管理對象
self.motionManager = [[CMMotionManager alloc] init];
// 2.判斷陀螺儀是否可用
if (![self.motionManager isGyroAvailable]) {
NSLog(@"陀螺儀不可用");
return;
}
// 3.開始更新
[self.motionManager startGyroUpdates];
}
//在需要的時候獲取值
- (void)getGyroData
{
CMRotationRate rotationRate = self.motionManager.gyroData.rotationRate;
NSLog(@"加速度 == x:%f, y:%f, z:%f", rotationRate.x, rotationRate.y, rotationRate.z);
}
三烫葬、demo
這里有我一個封裝好可以直接使用搖一搖的demo界弧,歡迎參考!4钭邸垢箕!