欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

OC:传感器

程序员文章站 2024-01-14 22:24:46
...
距离传感器

- (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(@"有物品离开");
    }
}

-(void)dealloc
{
    // 移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

加速计


- (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);
}

coreMotion


- (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;
}

摇一摇. 监听简单的手机摇动,利用系统的方法.

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

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

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

计步器,导入 :

#import <CoreMotion/CoreMotion.h>

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.判断计步器是否可用
    if (![CMStepCounter isStepCountingAvailable]) {
        NSLog(@"计步器不可用");
        return;
    }
    
    // 2.开始计步
    [self.counter startStepCountingUpdatesToQueue:[NSOperationQueue mainQueue] updateOn:5 withHandler:^(NSInteger numberOfSteps, NSDate *timestamp, NSError *error) {
        if (error) return;
        
        self.stepLabel.text = [NSString stringWithFormat:@"您一共走了%ld步", numberOfSteps];
    }];
}

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