iOS 开发iOS开发iOS_UIkit

iOS中传感器的基本使用

2015-08-21  本文已影响10226人  YotrolZ

iOS中常见的传感器

iOS中常见的传感器

一.距离传感器

[UIDevice currentDevice].proximityMonitoringEnabled = YES;


- 示例程序:


### 二.加速计(UIAccelerometer)
- 概述:
检测设备在`X/Y/Z轴`上的`受力`情况
![加速计](http:https://img.haomeiwen.com/i590107/98de475675ae93b7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- 监听方式:设置`代理`

- 使用步骤:(iOS5之前)
   - 获取加速计`单例`对象:

UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];


   - 设置加速计`代理`对象

accelerometer.delegate = self;


   - 设置`采样间隔` : `updateInterval`

accelerometer.updateInterval = 0.3;


   - 实现代理相关方法,监听加速计的数据
      - `UIAcceleration `参数:
         ```
@interface UIAcceleration : NSObject {
  @private
    NSTimeInterval timestamp;
    UIAccelerationValue x, y, z;
}
#import "ViewController.h"

@interface ViewController () <UIAccelerometerDelegate>

@end

@implementation ViewController

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

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

@end
UIAcceleration and UIAccelerometer are deprecated as of iOS 5.0. These classes have been replaced by the Core Motion framework.

三 . Core Motion

Core Motion的使用步骤---push
CMMotionManager*mgr = [[CMMotionManageralloc]init];
if(mgr.isAccelerometerAvailable){
    //加速计可用
}
mgr.accelerometerUpdateInterval= 1.0/30.0;// 1秒钟采样30次
-(void)startAccelerometerUpdatesToQueue:(NSOperationQueue*)queue withHandler:(CMAccelerometerHandler)handler;
#import "ViewController.h"

@interface ViewController () <UIAccelerometerDelegate>

/** 运动管理者 */
@property (nonatomic, strong) CMMotionManager *mgr; // 保证不死

@end

@implementation ViewController

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

    // 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;
        
        // 4.获取加速计信息
        CMAcceleration acceleration = accelerometerData.acceleration;
        NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
    }];
}
@end
Core Motion的使用步骤---pull
CMMotionManager*mgr = [[CMMotionManageralloc]init];
if(mgr.isAccelerometerAvailable){
    //加速计可用
}
-(void)startAccelerometerUpdates;
CMAcceleration acc = mgr.accelerometerData.acceleration;
NSLog(@"%f,%f, %f", acc.x,acc.y,acc.z);
#import "ViewController.h"

@interface ViewController () <UIAccelerometerDelegate>

/** 运动管理者 */
@property (nonatomic, strong) CMMotionManager *mgr; // 保证不死

@end

@implementation ViewController

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

    // 1.判断加速计是否可用
    if (!self.mgr.isAccelerometerAvailable) {
        NSLog(@"加速计不可用");
        return;
    }
    
    // 2.开始采样
    [self.mgr startAccelerometerUpdates];
}
@end

    // 3.数据采样(以点击了屏幕为例说明)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 获取加速计信息
    CMAcceleration acceleration = self.mgr.accelerometerData.acceleration;
    NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
}

四.磁力计/陀螺仪的使用和上述加速计的使用步骤类似

不同点:

@property(readonly, nonatomic, getter=isAccelerometerAvailable) BOOL accelerometerAvailable;
@property(readonly, nonatomic, getter=isGyroAvailable) BOOL gyroAvailable;
@property(readonly, nonatomic, getter=isMagnetometerAvailable) BOOL magnetometerAvailable
@property(assign, nonatomic) NSTimeInterval accelerometerUpdateInterval;
@property(assign, nonatomic) NSTimeInterval gyroUpdateInterval;
@property(assign, nonatomic) NSTimeInterval magnetometerUpdateInterval
 - (void)startAccelerometerUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMAccelerometerHandler)handler;
 - (void)startGyroUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMGyroHandler)handler;
 - (void)startMagnetometerUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMMagnetometerHandler)handler;
 - (void)startAccelerometerUpdates;
 - (void)startGyroUpdates;
 - (void)startMagnetometerUpdates;

   - 陀螺仪:
      ```
    CMRotationRate rate = self.mgr.gyroData.rotationRate;
    NSLog(@"x:%f y:%f z:%f", rate.x, rate.y, rate.z);

五.没事你就,摇一摇

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

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

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

六. 没事走两步(计步器)

    if (![CMPedometer isStepCountingAvailable]) {
        NSLog(@"计步器不可用");
        return;
    }
CMPedometer *stepCounter = [[CMPedometer alloc] init];
    [stepCounter startPedometerUpdatesFromDate:[NSDate date] withHandler:^(CMPedometerData *pedometerData, NSError *error) {
        if (error) return;
        // 4.获取采样数据
        NSLog(@"steps = %@", pedometerData.numberOfSteps);
    }];

七.蓝牙

上一篇下一篇

猜你喜欢

热点阅读