CoreMotion
大家可能都聽過陀螺儀 這個(gè)東西 是繼iphone4 之后蘋果推出的特別牛的東西,現(xiàn)在教教大家怎么獲取陀螺儀里的信息來做一些事情
距離傳感器(不是基于CoreMotion)框架
- (void)viewDidLoad {
[super viewDidLoad];
//打開傳感器
[UIDevice currentDevice].proximityMonitoringEnabled =YES;
//監(jiān)聽有物品靠近還是離開
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Change:) name:UIDeviceProximityStateDidChangeNotification object:nil];
}
-(void)Change:(NSNotificationCenter*)center{
if ([UIDevice currentDevice].proximityState) {
NSLog(@"物品靠近");
}else{
NSLog(@"物品離開");
}
}
//別忘了釋放掉
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
手機(jī)微信的搖一搖 (這個(gè)更簡單) 都知道TouchBegin方法吧
//開始搖一搖
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"用戶搖一搖");
}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
//搖一搖被打斷(比如搖的過程中來電話)
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
//搖一搖結(jié)束的時(shí)候操作
}
獲取蘋果計(jì)步器的信息(基于CoreMotion)
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *mylabel;
@property(nonatomic,strong)CMPedometer *step;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
if (![CMPedometer isStepCountingAvailable ]) {
NSLog(@"不可用");
return;
}
//開始計(jì)步
[self.step startPedometerUpdatesFromDate:[NSDate date] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
NSLog(@"%@",pedometerData.numberOfSteps);
self.mylabel.text =[NSString stringWithFormat:@"%@",pedometerData.numberOfSteps];
}];
}
下面的代碼比較長但是沒有復(fù)雜的邏輯 ,因?yàn)椴皇呛軓?fù)雜就寫得一起了,利用CoreMotion 實(shí)現(xiàn)加速器,陀螺儀,磁力傳感器(用于導(dǎo)航,海航)
#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property(nonatomic,strong)CMMotionManager *manger;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//獲取磁力計(jì)傳感器
//1.判斷磁力計(jì)是否可用
if (![self.manger isMagnetometerAvailable]) {
return;
}
//2.設(shè)置采樣間隔
self.manger.magnetometerUpdateInterval =1.0;
//3開始獲取
[self.manger startMagnetometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMMagnetometerData * _Nullable magnetometerData, NSError * _Nullable error) {
CMMagneticField filed = magnetometerData.magneticField;
NSLog(@"磁力計(jì) %f,%f,%f",filed.x,filed.y,filed.z);
}];
//獲取陀螺儀的方法
[self getGyro];
//獲取加速計(jì)的方法
[self getAccelerometer];
}
-(void)getGyro{
//先判斷陀螺儀是否可用
if (![self.manger isGyroAvailable]) {
NSLog(@"陀螺儀不可用");
return;
}
//2 設(shè)置采樣間隔
self.manger.gyroUpdateInterval =0.2;
[self.manger startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
CMRotationRate rate = gyroData.rotationRate;
//獲取陀螺儀 三個(gè)xyz值
NSLog(@"x:%f y:%f z:%f", rate.x, rate.y, rate.z);
}];
}
-(void)getAccelerometer{
if (![self.manger isAccelerometerAvailable]) {
NSLog(@"加速計(jì)不可用");
}
//設(shè)置采取 時(shí)間間隔
self.manger.accelerometerUpdateInterval =0.1;
[self.manger startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
// 獲取加速計(jì)信息
CMAcceleration acceleration = accelerometerData.acceleration;
NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
}];
}
@end