CoreMotion

距离传感器

#import "ViewController.h"

@interface ViewController ()

@end


@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];

 // 打开距离传感器
 [UIDevice currentDevice].proximityMonitoringEnabled = YES;

 // 监听距离传感器值的变化 只能用真机来监测
 [[NSNotificationCenter defaultCenter] addObserver:self 
                                          selector:@selector(deviceProximityStateDidChangeNotification) 
                                              name:UIDeviceProximityStateDidChangeNotification 
                                            object:nil];
}

/**
 * 实现通知绑定的方法
 */
- (void)deviceProximityStateDidChangeNotification {

 // 因为距离传感器比较特殊 不需要通知的值
 if ([UIDevice currentDevice].proximityState) {

   NSLog(@"有人靠近...");

 } else {

  NSLog(@"那人走了...");
 }

}

/**
 * 移除观察者
 */
- (void)dealloc {
 [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                 name:UIDeviceProximityStateDidChangeNotification 
                                               object:nil];
}

运动管理器


#import "ViewController.h"

#import <CoreMotion/CoreMotion.h>

@interface ViewController ()

/** 运动管理器*/
@property (nonatomic, strong) CMMotionManager *motionManager;

@end


@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];

 /**
 运动管理器包括
 1. 加速计 : 检测力在某个方向上的作用 : Accelerometer
 2. 陀螺仪 : 检测转动的角速度 : Gyro
 3. 磁力计 : 检测磁场的
 */

 /**
 PUSH方式的磁力计
 */

 //1. 创建运动管理器
 self.motionManager = [CMMotionManager new];

 //2. 判断能否使用磁力计
 if (![self.motionManager isMagnetometerAvailable]) {
 return;
 }

 //3. 采样间隔/ 更新间隔 --> 只有push方式才需要设置 --> 秒为单位
 self.motionManager.magnetometerUpdateInterval = 1;

 //4. 开始采样 --> push方式使用此方式
 [self.motionManager startMagnetometerUpdatesToQueue:
[NSOperationQueue mainQueue] withHandler:^(CMMagnetometerData * _Nullable magnetometerData,
                                            NSError * _Nullable error) {
 //单位: 特斯拉
 CMMagneticField magneticField = magnetometerData.magneticField;

 NSLog(@"x: %f, y: %f, z: %f", magneticField.x, magneticField.y, magneticField.z);

 }];

}

#pragma mark 点击屏幕获取数据
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

// accelerometerData: 加速计检测到的值, 会自动传递给运动管理器的属性
// CMAcceleration acceleration = self.motionManager.accelerometerData.acceleration;
// NSLog(@"x: %f, y: %f, z: %f", acceleration.x, acceleration.y, acceleration.z);

 //accelerometerData: 加速计检测到的值, 会自动传递给运动管理器的属性
 CMRotationRate rotationRate = self.motionManager.gyroData.rotationRate;
 NSLog(@"x: %f, y: %f, z: %f", rotationRate.x, rotationRate.y, rotationRate.z);
}

#pragma mark 加速计Push
- (void)acceleratePush {
 /**
 PUSH方式的加速计
 */

 //1. 创建运动管理器
 self.motionManager = [CMMotionManager new];

 //2. 判断能否使用加速计
 if (![self.motionManager isAccelerometerAvailable]) {
 return;
 }

 //3. 采样间隔/ 更新间隔 --> 只有push方式才需要设置 --> 秒为单位
 self.motionManager.accelerometerUpdateInterval = 1;

 //4. 开始采样 --> push方式使用此方式
 [self.motionManager startAccelerometerUpdatesToQueue:
       [NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable 
                                    accelerometerData, NSError * _Nullable error) {

 //4.1 获取加速计值的对象
 CMAcceleration acceleration = accelerometerData.acceleration;

 /**
 加速计: 加速度 --> 力在某个方向上的作用
 1. 轴指向地面的方向, 那么那个方向的值就会获取出来
 2. 默认的值, 在-1~1之间. 如果在某个轴上用力了, 那么那个轴的值就会变大
 */

 NSLog(@"x: %f, y: %f, z: %f", acceleration.x, acceleration.y, acceleration.z);

 }];

}


#pragma mark 加速计Pull
- (void)acceleratePull {
 /**
 PULL方式的加速计 : 当需要获取数据的时候, 才去获取数据 , 不需要设置更新间隔
 */

 //1. 创建运动管理器
 self.motionManager = [CMMotionManager new];

 //2. 判断能否使用加速计
 if (![self.motionManager isAccelerometerAvailable]) {
 return;
 }

 //3. 开始采样
 [self.motionManager startAccelerometerUpdates];
}

#pragma mark 陀螺仪Push
- (void)gyroPush {

 /**
 PUSH方式的陀螺仪
 */

 //1. 创建运动管理器
 self.motionManager = [CMMotionManager new];

 //2. 判断能否使用陀螺仪
 if (![self.motionManager isGyroAvailable]) {
 return;
 }

 //3. 采样间隔/ 更新间隔 --> 只有push方式才需要设置 --> 秒为单位
 self.motionManager.gyroUpdateInterval = 1;

 //4. 开始采样 --> push方式使用此方式
 [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue] 
               withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {

 //rotationRate: 旋转速率
 CMRotationRate rotationRate = gyroData.rotationRate;

 //只有转动手机, 才会有值得获取
 NSLog(@"x: %f, y: %f, z: %f", rotationRate.x, rotationRate.y, rotationRate.z);

 }];

}

#pragma mark 陀螺仪Pull
- (void)gyroPull {

 /**
 PULL方式的陀螺仪
 */

 //1. 创建运动管理器
 self.motionManager = [CMMotionManager new];

 //2. 判断能否使用陀螺仪
 if (![self.motionManager isGyroAvailable]) {
 return;
 }

 //3. 开始采样
 [self.motionManager startGyroUpdates];
}

@end

摇一摇

#import "ViewController.h"

@interface ViewController ()

@end


@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];

}

/**
 * 摇一摇 加速器的一种应用
   可以利用加速器的值来判断是否发生了摇动
 */



- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
 // 开始时 做界面处理 发送网络请求等...
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
 // 将获取到的数据 显示到界面上...
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {

}

计步器

#import "ViewController.h"

#import <CoreMotion/CoreMotion.h>

@interface ViewController ()

/** iOS7计步器*/
@property (nonatomic, strong) CMStepCounter *stepCounter;

/** iOS8计步器*/
@property (nonatomic, strong) CMPedometer *pedometer;

@property (weak, nonatomic) IBOutlet UILabel *label;

@end


@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];

 /**
   计步器: 加速计的应用
  */

 //iOS8开始出现的计步器

 //1. 判断计步器是否可用
 if (![CMPedometer isStepCountingAvailable]) {
 return;
 }

 //2. 创建计步器
 self.pedometer = [CMPedometer new];

 //3. 开始计步 --> iOS8没有显示的写队列 --> 默认不在主队列
 [self.pedometer startPedometerUpdatesFromDate:[NSDate date] 
         withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {

 NSNumber *number = pedometerData.numberOfSteps;
 NSLog(@"number: %@",number);

 //在主队列更新界面
 [self performSelectorOnMainThread:@selector(updateUI:) withObject:number waitUntilDone:NO];

 }];
}

#pragma mark 更新界面的方法
- (void)updateUI:(NSNumber *)number {
 self.label.text = [NSString stringWithFormat:@"您当前一共走了%@步", number];
}

#pragma mark iOS7的计步器
- (void)setpCounter {

 //iOS7出现~iOS8过期

 //1. 判断计步器是否可用
 if (![CMStepCounter isStepCountingAvailable]) {
 return;
 }

 //2. 创建计步器
 self.stepCounter = [CMStepCounter new];

 //3. 开始计步
 //updateOn: 非常不准
 [self.stepCounter startStepCountingUpdatesToQueue:[NSOperationQueue mainQueue] updateOn:5 
     withHandler:^(NSInteger numberOfSteps, NSDate * _Nonnull timestamp, NSError * _Nullable error) {

 NSLog(@"number:%zd", numberOfSteps);
 }];

}

@end

results matching ""

    No results matching ""