iOS开发传感器相关

手机里面内置了很多的传感器,例如:光传感器,湿度传感器,温度传感器,距离传感器等等

//开发传感器相关的东西必须使用真机

//在螺旋仪和加速计所有两种方式push和pull的方式,push的方式是时时检测,pull的方式是需要时获取检测值

/*

加速计push的使用步骤:

1.创建运动管理者

_mgr = [[CMMotionManager alloc] init];

2.判断手机加速计是否可用

if (!self.mgr.isAccelerometerAvailable) {

NSLog(@"加速计不可使用,请更换手机");

return;

}

3.设置取样间隔

self.mgr.accelerometerUpdateInterval = 1.0/30.0;

4.开启检测

[self.mgr 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);

}];

2.陀螺仪pull方式使用步骤

1.创建运动管理者

_mgr = [[CMMotionManager alloc] init];

2.判断手机陀螺仪是否可用

if (!self.mgr.isGyroAvailable) {

NSLog(@"陀螺仪不可用");

return;

}

3.开启检测

[self.mgr startGyroUpdates];

4.获取需要的值

CMRotationRate   rotationRate =   self.mgr.gyroData.rotationRate;

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

//加速计和陀螺仪的用法基本一致--------------

//运动管理者控制器要去拥有它,否则有可能为局部变量,不能使用.

*/

//#import <CoreMotion/CoreMotion.h>导入与运动相关框架

#import "ViewController.h"

#import <CoreMotion/CoreMotion.h>

@interface ViewController ()

@property (nonatomic,strong) CMMotionManager * mgr;

@end

@implementation ViewController

//懒加载创建运动管理者

- (CMMotionManager *)mgr

{

if (_mgr == nil) {

_mgr = [[CMMotionManager alloc] init];

}

return _mgr;

}

- (void)viewDidLoad {

[super viewDidLoad];

}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

NSLog(@"%zd,%@",motion,event);

NSLog(@"开始摇晃");

}

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

{

NSLog(@"%zd,%@",motion,event);

NSLog(@"摇晃取消");

}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

NSLog(@"%zd,%@",motion,event);

NSLog(@"摇晃结束");

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

//加速计相关

//    CMAcceleration   acceleration =    self.mgr.accelerometerData.acceleration;

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

//加速计相关

//陀螺仪相关

CMRotationRate   rotationRate =   self.mgr.gyroData.rotationRate;

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

}

- (void)pullGyro

{

if (!self.mgr.isGyroAvailable) {

NSLog(@"陀螺仪不可用");

return;

}

[self.mgr startGyroUpdates];

}

- (void)pushGyro

{

//判断手机的陀螺仪是否可用

if (!self.mgr.gyroAvailable) {

NSLog(@"陀螺仪不可用,请更换手机");

return;

}

//设置采样间隔

self.mgr.gyroUpdateInterval = 1.0/20.0;

//开始检测陀螺仪的变化

[self.mgr startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {

CMRotationRate  rotationRate =   self.mgr.gyroData.rotationRate;

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

}];

}

- (void)pullAccelerometer

{

if (!self.mgr.isAccelerometerAvailable) {

NSLog(@"加速计不可使用,请更换手机");

return;

}

[self.mgr startAccelerometerUpdates];

}

- (void)pushAccelerometer

{

//CMMotionManager * mgr = [[CMMotionManager alloc] init];

//判断加速计是否可用

if (!self.mgr.isAccelerometerAvailable) {

NSLog(@"加速计不可用,请更换手机");

return;

}

//设置采样间隔

self.mgr.accelerometerUpdateInterval = 1.0/30.0;

[self.mgr 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);

}];

}

- (void)proximityMonitoring

{

//开启距离传感器

[UIDevice currentDevice].proximityMonitoringEnabled = YES;

//监听距离的变化

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];

}

- (void)proximityStateDidChange:(NSNotification*)noti

{

if ([UIDevice currentDevice].proximityState == YES) {

NSLog(@"有物品靠近");

}else{

NSLog(@"物品离开");

}

}

- (void)dealloc

{

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

上一篇:QtGUI Module's Classes


下一篇:Python3基础 os listdir curdir pardir 查看工作目录及其上一级目录的所有文件名