传感器应用集合

来源:互联网 发布:乐高机器人的所有编程 编辑:程序博客网 时间:2024/05/21 20:22


距离传感器


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    /**

     使用场景:类似微信的语音播放, 根据距离传感器来判断使用什么东西播放

     */

    

    //1. 打开距离传感器

    //proximityMonitoringEnabled: 距离检测可用

    [UIDevicecurrentDevice].proximityMonitoringEnabled =YES;

    

    //2. 添加通知

    [[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(proximityStateDidChangeNotification)name:UIDeviceProximityStateDidChangeNotificationobject:nil];

}


#pragma mark 通知绑定的方法

- (void)proximityStateDidChangeNotification {

    if ([UIDevicecurrentDevice].proximityState) {

        NSLog(@"有逗逼靠近了");

    } else {

        NSLog(@"逗逼被吓跑了");

    }

}


@end


=================================================================

PUSH是实时采集数据,PULL是在需要的时候再去主动采集数据
Coremotion.frameWork

    /**

     .加速计 : 检测力在某个方向上有作用 (加速度)

     使用场景: 摇一摇  / 计步器

     */



PUSH方式实现加速计,

 (void)acceleromatePush {

    // push方式 -->系统会按照设置好的更新间隔, 不断的返回数据

    //1. 创建运动管理器

    self.motionManager = [CMMotionManagernew];

    

    //2. 判断加速计是否可用 iPhone4

    if (![self.motionManagerisAccelerometerAvailable]) {

        NSLog(@"硬件损坏或者没有对应的传感器");

        return;

    }

    

    //3. 设置采样间隔 单位是秒

    self.motionManager.accelerometerUpdateInterval =1;

    

    //4. 开始采样 --> 适用于push方式

    [self.motionManagerstartAccelerometerUpdatesToQueue:[NSOperationQueuemainQueue] withHandler:^(CMAccelerometerData *_Nullable accelerometerData,NSError * _Nullable error) {

        

        // Z轴的负方向指向了地面,所以打印出了-z

        

        //1. 打印的值,是轴的负方向的值. 那个轴的方向指向了地面,就会打印哪个方向的值

        

        //2. 在某个轴上运动的越快, 值会越大.默认不动 1~-1;

        

        CMAcceleration acceleration = accelerometerData.acceleration;

        

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

    }];

}



puLL方式实现加速计,持续采样

- (void)acceleratePull

{

    //pull方式 -->在需要的时候, 才获取加速计的数据值

    //1. 创建运动管理器

    self.motionManager = [CMMotionManagernew];

    

    //2. 判断加速计是否可用 iPhone4

    if (![self.motionManagerisAccelerometerAvailable]) {

        NSLog(@"硬件损坏或者没有对应的传感器");

        return;

    }

    

    //3. 开始采样 -->适用于pull方式,一旦采样所有的数据都保存在运动管理器中,不需要代理方法

    [self.motionManagerstartAccelerometerUpdates];

    CMAccelerometerData *accelerometerData = self.motionManager.accelerometerData;

//    

//    CMAcceleration acceleration = accelerometerData.acceleration;

//    

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



}


============================================================



push方式实现陀螺仪:

    /**

     .陀螺仪 :检测转动的角速度  (转动角速度)

     */


- (void)gyroPush

{

    //push方式

    //1. 创建运动管理器

    self.motionManager = [CMMotionManagernew];

    

    //2. 判断陀螺仪是否可用

    if (![self.motionManagerisGyroAvailable]) {

        NSLog(@"硬件损坏或者没有对应的传感器");

        return;

    }

    

    //3. 设置采样间隔

    self.motionManager.gyroUpdateInterval =1;

    

    //4. 开始采样

    [self.motionManagerstartGyroUpdatesToQueue:[NSOperationQueuemainQueue] withHandler:^(CMGyroData *_Nullable gyroData,NSError * _Nullable error) {

        

        // 陀螺仪的值得打印,手机沿着哪个轴转动, 那个轴的值就会发生变化.

        // 如果不发生转动, 值基本上为0

        CMRotationRate rotationRate = gyroData.rotationRate;

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

    }];


}

PULL方式实现陀螺仪

- (void)gyroPull

{

    //pull方式

    //1. 创建运动管理器

    self.motionManager = [CMMotionManagernew];

    

    //2. 判断陀螺仪是否可用

    if (![self.motionManagerisGyroAvailable]) {

        NSLog(@"硬件损坏或者没有对应的传感器");

        return;

    }

    

    //3.开始采样

    [self.motionManagerstartGyroUpdates];

//2. 陀螺仪的值

//    CMGyroData *gyroData = self.motionManager.gyroData;

//    

//    CMRotationRate rotationRate = gyroData.rotationRate;

//    

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


}



=========================================================

/**

     .磁力计 :检测磁场的变化

     */

    

    //pull方式

    //1. 创建运动管理器

    self.motionManager = [CMMotionManagernew];

    

    //2. 判断陀螺仪是否可用

    if (![self.motionManagerisMagnetometerAvailable]) {

        NSLog(@"硬件损坏或者没有对应的传感器");

        return;

    }

    

    //3.开始采样

    [self.motionManagerstartMagnetometerUpdates];


     //3. 磁力计的值

    CMMagnetometerData *magnetometerData = self.motionManager.magnetometerData;

    

    // 磁力计的单位: 特斯拉

    CMMagneticField magneticField = magnetometerData.magneticField;

    

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

=========================================================

摇一摇的实现

    /**

     实现摇一摇的方式

     1. 加速计可以实现 x, y, z .可以判断3个轴的值,任何一个轴的值大于某个特定的值(5)

     2. 使用系统封装好的方法

     */


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

{

    //微信:摇动开始, 2张图像张开

    NSLog(@"开始摇一摇");

}


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

{

    NSLog(@"摇动取消");

}


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

{

    //微信:摇动结束: 弹出摇到的人

    NSLog(@"摇完了,摇到到lb");

}


=============================================================


计步器的使用



#import "ViewController.h"

#import <CoreMotion/CoreMotion.h>


@interface ViewController ()


/** iOS7计步器*/

@property (nonatomic,strong) CMStepCounter *setpCounter;


/** iOS8以后的计步器*/

@property (nonatomic,strong) CMPedometer *pedomete;


@property (weak, nonatomic) IBOutletUILabel *label;


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    //1. 判断计步器是否可用

    if (![CMPedometerisStepCountingAvailable]) {

        return;

    }

    

    //2. 创建计步器

    self.pedomete = [CMPedometernew];

    

    //3. 开始统计步数

    //FromDate: 开始统计的时间

    [self.pedometestartPedometerUpdatesFromDate:[NSDatedate] withHandler:^(CMPedometerData *_Nullable pedometerData, NSError * _Nullable error) {

        

        // 计步器的数据并不是实时返回的,会有一定的延迟

        //更新界面 -->主线程

        [selfperformSelectorOnMainThread:@selector(updateUI:)withObject:pedometerData.numberOfStepswaitUntilDone:NO];

        

        NSLog(@"当前走了%@", pedometerData.numberOfSteps);

    }];

}


#pragma mark 更新界面

- (void)updateUI:(NSNumber *)number

{

    self.label.text = [NSStringstringWithFormat:@"当前一共走了%@", number];

}


#pragma mark iOS7 ~iOS8 使用的计步器

- (void)stepCounter {

    //1. 判断计步器是否可用

    if (![CMStepCounterisStepCountingAvailable]) {

        return;

    }

    

    //2. 创建计步器

    self.setpCounter = [CMStepCounternew];

    

    //3. 开始统计补数

    [self.setpCounterstartStepCountingUpdatesToQueue:[NSOperationQueuemainQueue] updateOn:5withHandler:^(NSInteger numberOfSteps,NSDate * _Nonnull timestamp,NSError * _Nullable error) {

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

    }];

    

}


@end


==========================================================

0 0
原创粉丝点击