iOS中 陀螺仪/加速器 韩俊强的博客
来源:互联网 发布:淘宝网切糕 编辑:程序博客网 时间:2024/04/30 20:53
引进框架:
加速器的使用:
晃动触发的一些方法:
每日更新关注:http://weibo.com/hanjunqiang 新浪微博
#import <CoreMotion/CoreMotion.h>
定义属性初始化相关:
#import "ViewController.h"#import <CoreMotion/CoreMotion.h>@interface ViewController ()@property (nonatomic, strong) CMMotionManager *motionManager;@property (nonatomic, strong) NSOperationQueue *quene;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // 初始化 CMMotionManager self.motionManager = [[CMMotionManager alloc]init]; // 初始化 NSOperationQueue self.quene = [[NSOperationQueue alloc]init]; // 调用加速器 [self configureAccelerometer]; // 调用陀螺仪 [self configureGrro]; }每日更新关注:http://weibo.com/hanjunqiang 新浪微博
加速器的使用:
/*// 每一个设备晃动的时候, 系统通知每一个在用的设备, 可以使本身成为第一响应者- (BOOL)canBecomeFirstResponder{ return YES;}- (void)viewDidAppear:(BOOL)animated{ [self becomeFirstResponder];} */// 加速器的方法- (void)configureAccelerometer{ /** * 5.0之前使用的是pull方式,之后使用push方式 * // pull 方式 // 判断加速器是否可以使用 if ([_motionManager isAccelerometerAvailable]) { [_motionManager setAccelerometerUpdateInterval:1 / 40.0]; [_motionManager startAccelerometerUpdates]; }else{ NSLog(@"加速器不能使用"); } */ // push 方式 if ([_motionManager isAccelerometerAvailable]) { // 设置加速器的频率 [_motionManager setAccelerometerUpdateInterval:1 / 40.0]; // 开始采集数据 [_motionManager startAccelerometerUpdatesToQueue:_quene withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { if (fabs(accelerometerData.acceleration.x) > 2.0 || fabs(accelerometerData.acceleration.y) > 2.0 || fabs(accelerometerData.acceleration.z) > 2.0) { NSLog(@"检测到震动"); } NSLog(@"%.2f__%.2f__%.2f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z); }]; }else{ NSLog(@"加速器不能使用"); } }// 触摸结束的时候- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ CMAcceleration acceleration = _motionManager.accelerometerData.acceleration; NSLog(@"%.2f__%.2f__%.2f",acceleration.x,acceleration.y,acceleration.z);}
陀螺仪的使用:
// 陀螺仪的使用- (void)configureGrro{ if ([_motionManager isGyroAvailable]) { [self.motionManager startGyroUpdatesToQueue:_quene withHandler:^(CMGyroData *gyroData, NSError *error) { NSLog(@"%.2f__%.2f__%.2f",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z); }]; }else{ NSLog(@"陀螺仪不能使用"); }}
晃动触发的一些方法:
- (void)viewDidDisappear:(BOOL)animated{ [self.motionManager stopAccelerometerUpdates]; [self.motionManager stopGyroUpdates];}// 开始晃动的时候触发- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"开始晃动");}// 结束晃动的时候触发- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"晃动结束");}// 中断晃动的时候触发- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"取消晃动,晃动终止");}
每日更新关注:http://weibo.com/hanjunqiang 新浪微博
1 0
- iOS中 陀螺仪/加速器 韩俊强的博客
- iphone中加速器,陀螺仪,磁力计的使用和实现
- iOS中陀螺仪的使用
- 加速器,陀螺仪测量移动距离的方法
- ios中陀螺仪CoreMotion的使用
- ios中陀螺仪CoreMotion的使用
- ios中陀螺仪CoreMotion的用法
- ios中陀螺仪CoreMotion的使用
- ios中陀螺仪CoreMotion的用法
- ios中陀螺仪CoreMotion的用法
- ios中陀螺仪CoreMotion的用法
- ios中陀螺仪CoreMotion的用法
- 加速器陀螺仪基本原理
- 加速器陀螺仪及算法
- ios 7 陀螺仪的使用
- 21.0~21.5 加速器与陀螺仪 Core Motion
- ios中加速器和重力感应器
- IOS开发----CMDeviceMotion陀螺仪的使用
- EM算法初探
- Caffe + Ubuntu 14.04 64bit + CUDA 6.5 配置说明
- Cocos2Dv3.4在AppDelegate中不返回的情况及解决
- 从小白慢慢往上爬的历程--Java运算符
- 串口流控
- iOS中 陀螺仪/加速器 韩俊强的博客
- redis安装在以服务形式安装在linux上
- JavaScript 字符串(String)对象与Date(日期)对象
- 多态-接口回调Demo
- SQL Server: 2005/2014 普通备份与压缩备份对比
- 个人学习c++的真实经验
- VMware Workstation 10+Centos7(64位)共享文件夹问题
- Ultra Pull To Refresh实现知乎下拉刷新风格注意事项
- linux下安装java