iPhone加速计的简单认识

来源:互联网 发布:四大审计软件 编辑:程序博客网 时间:2024/04/28 11:31
加速计的作用
用于检测设备的运动(比如摇晃)

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

计步器


加速计程序的开发
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



0 0
原创粉丝点击