【iOS新加速计事件】
1、iOS5.0以前,可以使用UIAcceleration来监听加速计事件。
2、Bug iOS5.0以后,UIAccelerometerDelegate已经被depreacated,如下:
deprecated不是说不能说了,而是意味着在将来版本会删除,所以如果不想更新知识的话,就使用UIAccelerometer吧。更保险的方法是使用一个Timer来检查UIAcceleration,即不依赖于此Delegate回调。
3、针对iOS4.0以上版本,推荐使用CMMotionManager来监听加速计事件。涉及到下面几个方法:
4、其实,CMMotionManager也挺简单的,加速计的方法就这么几个。
陀螺仪(传感器)和加速计:
加速计和陀螺仪的值都是通过Core Motion框架访问的,此框架提供了CMMotionManager类。该类提供的所有数据都是用来描述用户如何移动设备的。
应用程序创建一个CMMotionManager实例,然后通过以下某种模式使用它:
1它可以在动作发生时执行一些代码
2 它可以时刻监视一个持续更新的结构,使你随时能够访问最新的值。
陀螺仪(传感器)和加速计的使用:
label属性检查器的Lines改为0,高度增加即可。
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak,nonatomic) IBOutlet UILabel *accelerometerLabel;
@property (weak,nonatomic) IBOutlet UILabel *gyroscopeLabel;
@end
.m文件:
#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property (strong,nonatomic) CMMotionManager *motionManager;
//@property (strong,nonatomic) NSOperationQueue *queue;//第一种
@property (strong,nonatomic) NSTimer *updateTime;
@end
@implementation ViewController
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.motionManager startAccelerometerUpdates];
[self.motionManager startGyroUpdates];
self.updateTime = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 target:self selector:@selector(updateDisplay) userInfo:nil repeats:YES];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self.motionManager stopAccelerometerUpdates];
[self.motionManager stopGyroUpdates];
[self.updateTime invalidate];
self.updateTime = nil;
}
-(void)updateDisplay{
if (self.motionManager.accelerometerAvailable) {
CMAccelerometerData *accelerometerData = self.motionManager.accelerometerData;
self.accelerometerLabel.text =[NSString stringWithFormat:@"Accelerometer\n---\n""x: %+.2f\ny: %+.2f\nz: %+.2f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z];
}
if (self.motionManager.gyroAvailable) {
CMGyroData *gyroData = self.motionManager.gyroData;
self.gyroscopeLabel.text = [NSString stringWithFormat:@"Gyroscope\n---\n""x: %+.2f\ny: %+.2f\nz: %+.2f",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z];
}
}
-(void)viewDidLoad{
[super viewDidLoad];
self.motionManager = [[CMMotionManager alloc]init];//动作管理器初始化
if (self.motionManager.accelerometerAvailable) {
self.motionManager.accelerometerUpdateInterval = 1.0/10.0; //0.1s更新一次
}else{
self.accelerometerLabel.text = @"This device has no accelerometer.";
}
if (self.motionManager.gyroAvailable) {
self.motionManager.gyroUpdateInterval = 1.0/10.0;
}else{
self.accelerometerLabel.text = @"This device has no Gyroscope.";
}
/*
self.queue = [[NSOperationQueue alloc]init];
if (self.motionManager.isAccelerometerAvailable) {//判断有没有加速计
self.motionManager.accelerometerUpdateInterval = 1.0/10.0; //0.1s更新一次
//告诉动作管理器开始报告加速计更新
[self.motionManager startAccelerometerUpdatesToQueue:self.queue withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
NSString *labelText;
if (error) {
[self.motionManager stopAccelerometerUpdates];
labelText = [NSString stringWithFormat:@"Accelerometer encounted error:%@",error];
}else{
labelText = [NSString stringWithFormat:@"Accelerometer\n---\n""x: %+.2f\ny: %+.2f\nz: %+.2f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z];
}
dispatch_async(dispatch_get_main_queue(),^{
self.accelerometerLabel.text = labelText;
});
}];
}else {
self.accelerometerLabel.text = @"This device has no accelerometer.";
}
if (self.motionManager.gyroAvailable) {//判断有没有陀螺仪
self.motionManager.gyroUpdateInterval = 1.0/10.0;
[self.motionManager startGyroUpdatesToQueue:self.queue withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
NSString *labelText;
if (error) {
[self.motionManager stopGyroUpdates];
labelText = [NSString stringWithFormat:@"Gyroscope encounted error:%@",error];
}else{
labelText = [NSString stringWithFormat:@"Gyroscope\n---\n""x: %+.2f\ny: %+.2f\nz: %+.2f",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z];
}
dispatch_async(dispatch_get_main_queue(),^{
self.gyroscopeLabel.text = labelText;
});
}];
}else{
self.accelerometerLabel.text = @"This device has no Gyroscope.";
}
*/
}
@end