美文网首页
IOS 传感器

IOS 传感器

作者: panzhangbao | 来源:发表于2017-06-05 16:59 被阅读7次
//
//  ViewController.m
//  Sensor
//
//  Created by panzhangbao on 2017/6/2.
//  Copyright © 2017年 panzhangbao. All rights reserved.
//

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>   // 计步器

@interface ViewController ()

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


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self proximitySensor];
    
//    [self magnetometerMethod];
//    [self gyroMethod];
//    [self accelerometerMethod];
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 1.距离传感器 ProximitySensor
- (void)proximitySensor
{
    // 1.开启距离传感器(注意: 默认情况距离传感器是关闭的)
    //  [UIApplication sharedApplication].proximitySensingEnabled = YES;
    // 只要开启之后, 就开始实时监听
    [UIDevice currentDevice].proximityMonitoringEnabled = YES;
    // 2.当监听到有物体靠近设备时系统会发出通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];
}

// 当监听到有物体靠近设备时调用
- (void)proximityStateDidChange:(NSNotification *)note
{
    if( [UIDevice currentDevice].proximityState){
        NSLog(@"有物体靠近");
    }else{
        NSLog(@"物体离开");
    }
}

#pragma mark - 2.运动传感器 - 系统 API
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"用户摇一摇");
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"摇一摇被打断");
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"摇一摇结束");
}

#pragma mark - 磁力计
- (void)magnetometerMethod
{
    // 获取磁力计传感器的值
    self.motionManager = [CMMotionManager new];
    // 1.判断磁力计是否可用
    if (!self.motionManager.isMagnetometerAvailable) {
        return;
    }
    
    // 2.设置采样间隔
    self.motionManager.magnetometerUpdateInterval = 0.3;
    
    // 3.开始采样
    [self.motionManager startMagnetometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMMagnetometerData *magnetometerData, NSError *error) {
        if (error) return;
        
        CMMagneticField field = magnetometerData.magneticField;
        NSLog(@"磁力计:x:%f y:%f z:%f", field.x, field.y, field.z);
    }];

}

#pragma mark - 陀螺仪
- (void)gyroMethod
{
    // push
    // 1.判断陀螺仪是否可用
    if (![self.motionManager isGyroAvailable]) {
        NSLog(@"手机该换了");
        return;
    }
    
    // 2.设置采样间隔
    self.motionManager.gyroUpdateInterval = 0.3;
    
    // 3.开始采样
    [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData *gyroData, NSError *error) {
        if (error) return;
        
        CMRotationRate rate = gyroData.rotationRate;
        NSLog(@"陀螺仪:x:%f y:%f z:%f", rate.x, rate.y, rate.z);
    }];
}

#pragma mark - 加速计
- (void)accelerometerMethod
{
    // push
    // 1.判断加速计是否可用
    if (!self.motionManager.isAccelerometerAvailable) {
        NSLog(@"加速计不可用");
        return;
    }
    
    // 2.设置采样间隔
    self.motionManager.accelerometerUpdateInterval = 0.3;
    
    // 3.开始采样
    [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { // 当采样到加速计信息时就会执行
        if (error) return;
        
        // 获取加速计信息
        CMAcceleration acceleration = accelerometerData.acceleration;
        NSLog(@"加速计:x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

相关文章

网友评论

      本文标题:IOS 传感器

      本文链接:https://www.haomeiwen.com/subject/bbtwfxtx.html