加速计
加速计用来检测手机受到加速度的方向和大小.但是主要用于重力感应,对于运动中得过受力情况就无法准确感应,所以对于运动过程中的感应方向,我们使用陀螺仪.
iPhone内置的加速计也叫方向感应器. 感应器通过跟踪期在X.Y.Z轴上面的重力加速度的方向,检测当前设备的方向,比如是平躺着还是倒立着,平面是向上还是向下,手持时iPhone的home键的方向. 还可以通过加速侦测晃动事件.
对应的类是UIAccelerometer
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
#pragma mark 配置加速计
//参考文档貌似说 我们使用加速计 只需要正确的设置UIAccelerometer 的代理 然后使用UIAccelerometer的方法 来获取
//各种数据
//创建一个加速计对象
UIAccelerometer *accele = [UIAccelerometer sharedAccelerometer];
//设置数据的更新事件
accele.updateInterval = 0.5;
//设置代理
accele.delegate = self;
//设置第一响应者
[self.view becomeFirstResponder];
#pragma mark 配置陀螺仪 说一下简单配置 具体怎么用 等我搞明白了
//#import <CoreMotion/CoreMotion.h> 这个框架下
CMMotionManager *motionManager = [[CMMotionManager alloc]init];
[motionManager startGyroUpdates];
//获取陀螺仪的空间移动 和 加速计类似
double x = motionManager.gyroData.rotationRate.x;
double y = motionManager.gyroData.rotationRate.y;
double z = motionManager.gyroData.rotationRate.z;
//当不需要用到陀螺仪的时候 和加速计 类似 也要移除
// [motionManager stopGyroUpdates]
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
//当这个页面消失时 若是不需要检测数据了 就把代理去掉
//要是不去掉 他会后台一只调用系统硬件更新数据 耗电. 跟后台定位会耗电一个道理
[UIAccelerometer sharedAccelerometer].delegate = nil;
}
// 实现UIAccelerometer的代理方法 获取数据 这个方法根据你设定的刷新时间 一直调用
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
//获取UIAccelerometer检测手机移动的数据
NSLog(@"%f,%f,%f",acceleration.x,acceleration.y,acceleration.z);
}
//用加速计侦测晃动事件,需要对加速计获取的数据进行处理才能进行 , 万幸这么处理不需要我们去做.UIResponse处理了这个工作.
//我们只需要使用他的方法即可
//开始晃动
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"开始晃动");
}
//结束晃动
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"结束晃动");
}
//晃动中断
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"晃动中断");
}
//由于窗口对象的第一响应者会收到所有的响应事件,为了确保应用 当前视图控制器类为第一响应者 重写如下两个方法
- (BOOL)canBecomeFirstResponder{
return YES;
}
//等研究明白了 在发详细的说明 用在地图上的导航