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

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

//在螺旋仪和加速计所有两种方式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];

}

iOS开发传感器相关的更多相关文章

  1. iOS 开发,相关网址

    iOS 开发,相关网址 说明 网址 注册开发者 https://developer.apple.com/cn/programs/enroll/ 未付费688个人开发账号真机调试测试教程 http:// ...

  2. iOS开发其他相关

    1.iOS开发行情 1.1 iOS系统各个版本的占比查询 2.Xcode的使用 开发软件下载 Xcode Help(官方) 2.1 Xcode面板 Xcode面板 2.2 Xcode版本新功能 Xco ...

  3. iOS开发,UITableView相关问题

    第一条:UITableViewCell 内容的设置 //文本放到最后 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArr.co ...

  4. iOS开发 传感器(加速计、摇一摇、计步器)

    一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...

  5. iOS 开发库相关(持续更新)

    01-给任意view添加毛玻璃效果 https://github.com/JagCesar/iOS-blur   02-浮动式的textfield输入框(可用于登录界面) https://github ...

  6. 【转】 iOS开发之打包上传到App Store——(一)各种证书的理解

    OK,有日子没写iOS开发的相关文章啦,主要是最近的精力都没在这上面,不过既然产品已经快要出来了,就有必要了解一下各种证书啥的(众所周知iOS的一堆证书可是很让人头大呀),最近确实被这个搞得头大,然后 ...

  7. Mac和iOS开发资源汇总

    小引 本文主要汇集一些苹果开发的资源,会经常更新,建议大家把这篇文章单独收藏(在浏览器中按command+D). 今天(2013年7月19日)收录了许多中文网站和博客.大家一定要去感受一下哦. 如果大 ...

  8. 【转】Mac和iOS开发资源汇总—更新于2013-07-19

    小引 本文主要汇集一些苹果开发的资源,会经常更新,建议大家把这篇文章单独收藏(在浏览器中按command+D). 今天(2013年7月19日)收录了许多中文网站和博客.大家一定要去感受一下哦. 如果大 ...

  9. (转载)Mac和iOS开发资源汇总—更新于2013-07-19

    (转载)http://beyondvincent.com/2013/07/18/resources-for-mac-and-ios-developers/ 小引 本文主要汇集一些苹果开发的资源,会经常 ...

随机推荐

  1. JS判断在哪个浏览器打开

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  2. ie7,IE8不支持document.getElmentsByClassName的问题

    if (!document.getElementsByClassName) { document.getElementsByClassName = function(className, elemen ...

  3. 基于Unity的Profiler性能分析

    A. WaitForTargetFPS:       Vsync(垂直同步)功能所,即显示当前帧的CPU等待时间    B. Overhead:       Profiler总体时间-所有单项的记录时 ...

  4. Django命令行相关命令 以及创建一个空白网页的步骤

    django相关命令行命令: django.admin.py是Django的一个用于管理任务的命令行工具,manage.py是对django-admin.py的简单包装,每个Django Projec ...

  5. CodeForces 670A Holidays

    简单题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #inclu ...

  6. CSS样式类型

    CSS样式可以写在哪些地方呢?从CSS 样式代码插入的形式来看基本可以分为以下3种:内联式.嵌入式和外部式三种.这一小节先来讲解内联式 (一)内联式样式 内联式css样式表就是把css代码直接写在现有 ...

  7. Signal and Slots

    Signal and Slots 用于对象之间通信. 它是 Qt 的核心特性之一, 并且也是Qt 与其它框架差别最大的部分. 概述 在GUI编程中, 如果我们改变了一个控件, 我们可能想其它控件知道: ...

  8. 递归——CPS(一)

    程序中为什么需要栈stack? 普通的程序中,接触到子程序和函数的概念,很直观地,调用子程序时,会首先停止当前做的事情,转而执行被调用的子程序,等子程序执行完成后,再捡起之前挂起的程序,这有可能会使用 ...

  9. 在GNU/Linux下设置与定时更换桌面壁纸

    1 简介 在电脑桌面设置一组可以定时更换的壁纸已经不是什么新奇的功能了.比如,Windows 7.KDE桌面环境都可以实现这样的效果.可是,自己目前使用的Debian系统并未安装KDE.GNOME这样 ...

  10. contentType设置类型导致ajax post data 获取不到数据

    ajax post  data  获取不到数据,注意 content-type的设置 .post/get关于 jQuery data 传递数据.网上各种获取不到数据,乱码之类的. 好吧今天我也遇到了, ...