CoreMotion.framework框架介紹
CoreMotion.framework
是iOS中的一個核心運動框架,它能夠滿足我們手機許多應用的一些需求,比如:
- 指南針
- 加速計:微信搖一搖
- 游戲中根據重力感應的操作
- 計步器:知道我們每天走了多少步
一、加速計
檢測設備在X、Y、Z軸上的加速度 (哪個方向有力的作用,哪個方向運動了)
根據加速度數值,就可以判斷出在各個方向上的作用力度
注:重力不可忽略!!
在iOS5之前我們用UIAccelerometer來獲取加速度,用法非常簡單,不過iOS5之后就過期了,就用CoreMotion.framework了。
注:必須真機測試,不適用于模擬器
使用方法
加速計的數據獲取方式有兩種:push和pull
push
:
提供一個線程管理器NSOperationQueue和一個回調Block,CoreMotion自動在每一個采樣數據到來的時候回調這個Block,進行處理。在這種情況下,Block中的操作會在你自己的主線程內執行。
pull
:
你必須主動去向CMMotionManager要數據,這個數據就是最近一次的采樣數據。你不去要,CMMotionManager就不會給你。
首先引入CoreMotion.framework框架框架
#import <CoreMotion/CoreMotion.h>
創建一個管理類的對象
@property (nonatomic, strong) CMMotionManager *motionManager;
1、使用push方式獲取加速計數據:
- (void)accelerometerPush
{
// 1.初始化運動管理對象
self.motionManager = [[CMMotionManager alloc] init];
// 2.判斷加速計是否可用
if (![self.motionManager isAccelerometerAvailable]) {
NSLog(@"加速計不可用");
return;
}
// 3.設置加速計更新頻率,以秒為單位
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方式獲取加速計數據:
- (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);
}
二、陀螺儀
陀螺儀的主要作用,是基于角動量守恒的原理,沿著某個特定的坐標軸測量旋轉速率。在使用中,陀螺儀的轉子在高速旋轉時,始終指向一個固定的方向,當運動物體的運動方向偏離預定方向時,陀螺儀就可以感受出來。
注:必須真機測試,不適用于模擬器
使用方法
陀螺儀的數據獲取方式同樣也有兩種:push和pull,跟加速計的方式一樣!
首先引入CoreMotion.framework框架框架
#import <CoreMotion/CoreMotion.h>
創建一個管理類的對象
@property (nonatomic, strong) CMMotionManager *motionManager;
1、使用push方式獲取陀螺儀數據:
- (void)gyroPush
{
// 1.初始化運動管理對象
self.motionManager = [[CMMotionManager alloc] init];
// 2.判斷陀螺儀是否可用
if (![self.motionManager isGyroAvailable]) {
NSLog(@"陀螺儀不可用");
return;
}
// 3.設置陀螺儀更新頻率,以秒為單位
self.motionManager.gyroUpdateInterval = 0.1;
// 4.開始實時獲取
[self.motionManager startGyroUpdatesToQueue:[[NSOperationQueue alloc] init] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
//獲取陀螺儀數據
CMRotationRate rotationRate = gyroData.rotationRate;
NSLog(@"加速度 == x:%f, y:%f, z:%f", rotationRate.x, rotationRate.y, rotationRate.z);
}];
}
2、使用pull方式獲取陀螺儀數據:
- (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,歡迎參考!!!