iOS - 传感器

传感器集锦:指纹识别、运动传感器、加速计、环境光感、距离传感器、磁力计、陀螺仪

1,距离传感器

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // [UIApplication sharedApplication].proximitySensingEnabled = YES;
    [UIDevice currentDevice].proximityMonitoringEnabled = YES;
    
    // 监听有物品靠近还是离开
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange) name:UIDeviceProximityStateDidChangeNotification object:nil];
}

- (void)proximityStateDidChange
{
    if ([UIDevice currentDevice].proximityState) {
        NSLog(@"有物品靠近");
    } else {
        NSLog(@"有物品离开");
    }
}

2加速计,哪个方向有受力的加速度

2.1,4.0以前Accelerometer

@interface ViewController () 

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.获取单例对象
    UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
    
    // 2.设置代理
    accelerometer.delegate = self;
    
    // 3.设置采样间隔
    [accelerometer setUpdateInterval:0.3];
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
}

2,24.0以后CoreMotion

push:主动把数据告诉外界
pull:需要时直接读取

#import 

@interface ViewController ()

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 获取磁力计传感器的值
    // 1.判断磁力计是否可用
    if (!self.mgr.isMagnetometerAvailable) {
        return;
    }
    
    // 2.设置采样间隔
    self.mgr.magnetometerUpdateInterval = 0.3;
    
    // 3.开始采样
    [self.mgr 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);
    }];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 1.获取加速计信息
    CMAcceleration acceleration = self.mgr.accelerometerData.acceleration;
    NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
    
    // 2.获取陀螺仪信息
    CMRotationRate rate = self.mgr.gyroData.rotationRate;
    NSLog(@"x:%f y:%f z:%f", rate.x, rate.y, rate.z);
}

#pragma mark - 获取陀螺仪信息
- (void)pullGyro
{
    // pull
    // 1.判断陀螺仪是否可用
    if (![self.mgr isGyroAvailable]) {
        NSLog(@"手机该换了");
        return;
    }
    
    // 2.开始采样
    [self.mgr startGyroUpdates];
}

- (void)pushGyro
{
    // push
    // 1.判断陀螺仪是否可用
    if (![self.mgr isGyroAvailable]) {
        NSLog(@"手机该换了");
        return;
    }
    
    // 2.设置采样间隔
    self.mgr.gyroUpdateInterval = 0.3;
    
    // 3.开始采样
    [self.mgr 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)pullAccelerometer
{
    // pull
    // 1.判断加速计是否可用
    if (!self.mgr.isAccelerometerAvailable) {
        NSLog(@"加速计不可用");
        return;
    }
    
    // 2.开始采样
    [self.mgr startAccelerometerUpdates];
}

- (void)pushAccelerometer
{
    // push
    // 1.判断加速计是否可用
    if (!self.mgr.isAccelerometerAvailable) {
        NSLog(@"加速计不可用");
        return;
    }
    
    // 2.设置采样间隔
    self.mgr.accelerometerUpdateInterval = 0.3;
    
    // 3.开始采样
    [self.mgr 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);
    }];
}

#pragma mark - 懒加载
- (CMMotionManager *)mgr
{
    if (_mgr == nil) {
        _mgr = [[CMMotionManager alloc] init];
    }
    return _mgr;
}

3,摇一摇

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"用户摇一摇");
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    // 摇一摇被打断(电话)
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    // 摇一摇结束
}

4,计步器

plist配置 Privacy - Motion Usage Description

#import 

@interface ViewController ()

/** 计步器对象 */
@property (nonatomic, strong) CMPedometer *counter;
@property (weak, nonatomic) IBOutlet UILabel *stepLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.判断计步器是否可用
    if (![CMPedometer isStepCountingAvailable]) {
        NSLog(@"计步器不可用");
        return;
    }
    // 2.开始计步
    [self.counter startPedometerUpdatesFromDate:[NSDate new] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
        self.stepLabel.text = [NSString stringWithFormat:@"您一共走了%@步",pedometerData.numberOfSteps];
    }];
}

#pragma mark - 懒加载代码
- (CMPedometer *)counter
{
    if (_counter == nil) {
        _counter = [[CMPedometer alloc] init];
    }
    return _counter;
}

@end


你可能感兴趣的:(iOS - 传感器)