Oc MotionManager(运动管理器)~demo
来源:互联网 发布:智能小区网络测试ping 编辑:程序博客网 时间:2024/05/18 00:47
控制器1:
#import "ViewController.h"#import <CoreMotion/CoreMotion.h>//核心运动框架@interface ViewController (){ NSTimer *updateTimer;//更新时间}//运动管理器@property (strong, nonatomic) CMMotionManager *motionManager;//加速度计的标签@property (strong, nonatomic) IBOutlet UILabel *accelerometerLabel;//陀螺的标签@property (strong, nonatomic) IBOutlet UILabel *gyroLabel;//磁强计标签@property (strong, nonatomic) IBOutlet UILabel *magnetometerLabel;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // 创建CMMotionManager对象 self.motionManager = [[CMMotionManager alloc] init]; // 如果CMMotionManager的支持获取加速度数据 if (self.motionManager.accelerometerAvailable) { [self.motionManager startAccelerometerUpdates]; }else{ NSLog(@"该设备不支持获取加速度数据!"); } // 如果CMMotionManager的支持获取陀螺仪数据 if (self.motionManager.gyroAvailable) { [self.motionManager startGyroUpdates]; }else{ NSLog(@"该设备不支持获取陀螺仪数据!"); } // 如果CMMotionManager的支持获取磁场数据 if (self.motionManager.magnetometerAvailable) { [self.motionManager startMagnetometerUpdates]; }else{ NSLog(@"该设备不支持获取磁场数据!"); }}#pragma mark - 视图将要显示的时候- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; // 启动定时器来周期性地轮询加速度、陀螺仪、磁场数据 updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateDisplay) userInfo:nil repeats:YES]; // ②}#pragma mark - 定时器回调- (void)updateDisplay{ // 如果CMMotionManager的加速度数据可用 if (self.motionManager.accelerometerAvailable) { // 主动请求获取加速度数据 CMAccelerometerData* accelerometerData = self.motionManager.accelerometerData; self.accelerometerLabel.text = [NSString stringWithFormat: @"加速度为\n-----------\nX轴: %+.2f\nY轴: %+.2f\nZ轴: %+.2f", accelerometerData.acceleration.x, accelerometerData.acceleration.y, accelerometerData.acceleration.z]; } // 如果CMMotionManager的陀螺仪数据可用 if (self.motionManager.gyroAvailable) { // 主动请求获取陀螺仪数据 CMGyroData* gyroData = self.motionManager.gyroData; self.gyroLabel.text = [NSString stringWithFormat: @"绕各轴的转速为\n--------\nX轴: %+.2f\nY轴: %+.2f\nZ轴: %+.2f", gyroData.rotationRate.x, gyroData.rotationRate.y, gyroData.rotationRate.z]; } // 如果CMMotionManager的磁场数据可用 if (self.motionManager.magnetometerAvailable) { // 主动请求获取磁场数据 CMMagnetometerData* magnetometerData = self.motionManager.magnetometerData; self.magnetometerLabel.text = [NSString stringWithFormat: @"磁场数据为\n--------\nX轴: %+.2f\nY轴: %+.2f\nZ轴: %+.2f", magnetometerData.magneticField .x, magnetometerData.magneticField .y, magnetometerData.magneticField .z]; }}@end
阅读全文
0 0
- Oc MotionManager(运动管理器)~demo
- iOS MotionManager(运动管理器)~demo
- 人物运动的demo
- 运动追踪 demo
- OC 文件管理器NSFileManager
- OC文件管理器
- 网格布局管理器Demo
- 沿曲线速率运动Demo
- 圆周运动(运动小DEMO)
- oc 调用 swift Demo
- Oc 正则表达式 demo
- Oc HandDraw(手绘)-demo
- AN GridView文件管理器Demo
- html5动画基础 Demo(div运动)
- iOS Calendar Demo OC + Swift
- OC-054.文件管理器 NSFileManager类
- Oc-语句总结(3)--NSFileManager 文件管理器
- DragGridDemo 频道管理器的使用(demo)
- 烟花爆炸
- Oc UIImagePickerController(拍照、拍摄照片、选取照片)~demo
- 【bzoj1863】[Zjoi2006]trouble 皇帝的烦恼
- 1-1统计数字问题
- C#中内存表DataTable中新增一行记录
- Oc MotionManager(运动管理器)~demo
- C++强制类型转换
- Oc HandDraw(手绘)-demo
- 运算符使用
- 夸克浏览器产品分析报告
- 使用eclipse构建springboot项目的解决的一些问题
- 第八周项目四__稀疏矩阵的三元组表示的实现及应用(1)
- Android 基于TCP协议的网络通信
- 自学自动化测试Python2.7.14+selenium 2.53.6 +pycharm