CoreMotion
大家可能都听过陀螺仪 这个东西 是继iphone4 之后苹果推出的特别牛的东西,现在教教大家怎么获取陀螺仪里的信息来做一些事情
距离传感器(不是基于CoreMotion)框架
- (void)viewDidLoad {
[super viewDidLoad];
//打开传感器
[UIDevice currentDevice].proximityMonitoringEnabled =YES;
//监听有物品靠近还是离开
[[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];
}
手机微信的摇一摇 (这个更简单) 都知道TouchBegin方法吧
//开始摇一摇
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"用户摇一摇");
}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
//摇一摇被打断(比如摇的过程中来电话)
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
//摇一摇结束的时候操作
}
获取苹果计步器的信息(基于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;
}
//开始计步
[self.step startPedometerUpdatesFromDate:[NSDate date] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
NSLog(@"%@",pedometerData.numberOfSteps);
self.mylabel.text =[NSString stringWithFormat:@"%@",pedometerData.numberOfSteps];
}];
}
下面的代码比较长但是没有复杂的逻辑 ,因为不是很复杂就写得一起了,利用CoreMotion 实现加速器,陀螺仪,磁力传感器(用于导航,海航)
#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property(nonatomic,strong)CMMotionManager *manger;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取磁力计传感器
//1.判断磁力计是否可用
if (![self.manger isMagnetometerAvailable]) {
return;
}
//2.设置采样间隔
self.manger.magnetometerUpdateInterval =1.0;
//3开始获取
[self.manger startMagnetometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMMagnetometerData * _Nullable magnetometerData, NSError * _Nullable error) {
CMMagneticField filed = magnetometerData.magneticField;
NSLog(@"磁力计 %f,%f,%f",filed.x,filed.y,filed.z);
}];
//获取陀螺仪的方法
[self getGyro];
//获取加速计的方法
[self getAccelerometer];
}
-(void)getGyro{
//先判断陀螺仪是否可用
if (![self.manger isGyroAvailable]) {
NSLog(@"陀螺仪不可用");
return;
}
//2 设置采样间隔
self.manger.gyroUpdateInterval =0.2;
[self.manger startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
CMRotationRate rate = gyroData.rotationRate;
//获取陀螺仪 三个xyz值
NSLog(@"x:%f y:%f z:%f", rate.x, rate.y, rate.z);
}];
}
-(void)getAccelerometer{
if (![self.manger isAccelerometerAvailable]) {
NSLog(@"加速计不可用");
}
//设置采取 时间间隔
self.manger.accelerometerUpdateInterval =0.1;
[self.manger startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
// 获取加速计信息
CMAcceleration acceleration = accelerometerData.acceleration;
NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
}];
}
@end
网友评论