iPhone加速计的简单认识

加速计的作用
用于检测设备的运动(比如摇晃)

加速计的经典应用场景
摇一摇

计步器


加速计程序的开发
iOS4以前:使用UIAccelerometer,用法非常简单(到了iOS5就已经过期)
iOS4开始:CoreMotion.framework
虽然UIAccelerometer已经过期,但由于其用法极其简单,很多程序里面都还有残留
#import "ViewController.h"

@interface ViewController () <UIAccelerometerDelegate>

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

@end
iOS4之前,加速度计由UIAccelerometer类来负责采集数据
随着iPhone4的推出
加速度计全面升级,并引入了陀螺仪
Motion运动相关的编程成为重头戏
苹果特地在iOS4中增加了专门处理Motion的框架-CoreMotion.framework
Core Motion不仅能够提供实时的加速度值和旋转速度值,更重要的是,苹果在其中集成了很多牛逼的算法

Core Motion的使用步骤(push):
//
//  ViewController.m


#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>

@interface ViewController ()

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

@end

@implementation ViewController
<pre name="code" class="objc" style="color: rgb(127, 127, 127);">#pragma mark - 懒加载
- (CMMotionManager *)mgr
{
    if (_mgr == nil) {
        _mgr = [[CMMotionManager alloc] init];
    }
    return _mgr;
}
- (void)viewDidLoad { [super viewDidLoad];}// Core Motion的使用步骤(push)- (void)pushAccelerometer{ // 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); }];}

@end
<span style="white-space:pre"></span><pre name="code" class="objc" style="color: rgb(127, 127, 127);">Core Motion的使用步骤(pull)<span style="font-family: Arial, Helvetica, sans-serif;">	</span>

//
//  ViewController.m


#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>

@interface ViewController ()

/** 运动管理 */
@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];

}



#pragma mark - 获取加速计信息
// Core Motion的使用步骤(pull)
- (void)pullAccelerometer
{
    // 1.判断加速计是否可用
    if (!self.mgr.isAccelerometerAvailable) {
        NSLog(@"加速计不可用");
        return;
    }
    
    // 2.开始采样
    [self.mgr startAccelerometerUpdates];
    
    // 3.在需要的时候采集加速度数据(这里是用户touch之后)
}

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

@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值