iOS MotionManager(运动管理器)~demo
来源:互联网 发布:爱言叶2动作数据 编辑:程序博客网 时间:2024/05/18 13:43
//联系人:石虎 QQ: 1224614774昵称:嗡嘛呢叭咪哄
/**
注意点: 1.看 GIF 效果图.
2.看连线视图的效果图.
3.看实现代码(直接复制实现效果).
4.需要真机设备 + 硬件设备 ...
*/
一、GIF 效果图:
二、连线视图的效果图:
图1:
图2:
三、实现代码:
=========================
===================================================
==========================控制器1:
//
// ViewController.m
// MotionManager(运动管理器)~demo
//
// Created by 石虎 on 2017/8/14.
// Copyright © 2017年 shihu. All rights reserved.
//
#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>//核心运动框架
@interface ViewController ()
{
NSTimer *updateTimer;//更新时间
}
//运动管理器
@property (strong,nonatomic) CMMotionManager *motionManager;
//加速度计的标签
@property (strong,nonatomic) IBOutletUILabel *accelerometerLabel;
//陀螺的标签
@property (strong,nonatomic) IBOutletUILabel *gyroLabel;
//磁强计标签
@property (strong,nonatomic) IBOutletUILabel *magnetometerLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// 创建CMMotionManager对象
self.motionManager = [[CMMotionManageralloc] init];
// 如果CMMotionManager的支持获取加速度数据
if (self.motionManager.accelerometerAvailable)
{
[self.motionManagerstartAccelerometerUpdates];
}else{
NSLog(@"该设备不支持获取加速度数据!");
}
// 如果CMMotionManager的支持获取陀螺仪数据
if (self.motionManager.gyroAvailable)
{
[self.motionManagerstartGyroUpdates];
}else{
NSLog(@"该设备不支持获取陀螺仪数据!");
}
// 如果CMMotionManager的支持获取磁场数据
if (self.motionManager.magnetometerAvailable)
{
[self.motionManagerstartMagnetometerUpdates];
}else{
NSLog(@"该设备不支持获取磁场数据!");
}
}
#pragma mark - 视图将要显示的时候
- (void)viewWillAppear:(BOOL)animated
{
[superviewWillAppear:animated];
// 启动定时器来周期性地轮询加速度、陀螺仪、磁场数据
updateTimer = [NSTimerscheduledTimerWithTimeInterval:0.1
target:selfselector:@selector(updateDisplay)
userInfo:nilrepeats:YES]; // ②
}
#pragma mark - 定时器回调
- (void)updateDisplay
{
// 如果CMMotionManager的加速度数据可用
if (self.motionManager.accelerometerAvailable)
{
// 主动请求获取加速度数据
CMAccelerometerData* accelerometerData =self.motionManager.accelerometerData;
self.accelerometerLabel.text = [NSStringstringWithFormat:
@"加速度为\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 = [NSStringstringWithFormat:
@"绕各轴的转速为\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 = [NSStringstringWithFormat:
@"磁场数据为\n--------\nX轴: %+.2f\nY轴: %+.2f\nZ轴: %+.2f",
magnetometerData.magneticField .x,
magnetometerData.magneticField .y,
magnetometerData.magneticField .z];
}
}
@end
=========================
===================================================
谢谢!!!
- iOS MotionManager(运动管理器)~demo
- Oc MotionManager(运动管理器)~demo
- 人物运动的demo
- 运动追踪 demo
- 网格布局管理器Demo
- 沿曲线速率运动Demo
- 圆周运动(运动小DEMO)
- AN GridView文件管理器Demo
- iOS运动视觉效果-UIMotionEffect
- iOS运动事件学习
- iOS中的运动事件
- Ios demo
- IOS Demo
- html5动画基础 Demo(div运动)
- iOS xCode插件管理器
- iOS 之文件管理器
- IOS 开发上传管理器
- IOS文件系统,文件管理器
- Java集合---ConcurrentHashMap原理分析
- Unity 回合制战斗系统(中级篇)-进阶
- mysql alter 语句用法,添加、修改、删除字段等
- 《去哪网编程题》统计字符
- jQuery动画的hover连续触发动画bug处理
- iOS MotionManager(运动管理器)~demo
- 洛谷3816
- 编解码技术
- 树结构练习——排序二叉树的中序遍历
- 修改 placeholder 样式,placeholder 换行 以及iOS 上设置placeholder被遮挡
- Linux下设计一个简单的线程池
- linux系统恢复
- jQuery全选、全不选、反选的简洁写法
- ZigBee TI ZStack CC2530 3.14 按键驱动01-轮询模式