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
- iPhone加速计的简单认识
- IPhone加速计
- iphone之加速计
- iphone之加速计
- iphone开发 加速计
- iphone手机晃动-----加速计
- iphone中加速计的使用 from cocoachina
- 加速计.陀螺仪的使用
- 加速计
- 加速计
- 加速计
- 加速计
- 加速计
- iPhone开发基础教程笔记(十五)--第十五章 加速计
- 基于智能手机的加速计进行计步实现。
- 基于智能手机的加速计进行计步实现。
- 基于智能手机的加速计进行计步实现。
- iOS加速计(陀螺仪)的使用
- iPhone距离传感器
- 概率,先验概率,后验概率
- Oc计数器基本操作
- 水星MW300R v2 路由器刷DD-Wrt 小记
- 【翻译自mos文章】将Oracle 12c数据库从标准版convert到企业版
- iPhone加速计的简单认识
- gulp入门
- 姫野珠世 - can know two close
- 智力题
- Stanford 机器学习 Week2 作业: Linear Regression
- Android权限Uri.parse
- VIC水文模型入门攻略(上) 将VIC跑起来
- poj 2796 Feel Good 单调栈/dp
- 假期小算法--堆排序(啊哈算法)