在移动端开发过程中,有时候会用到陀螺仪传感器获取当前手机的姿态,下面给出iOS端如何获取陀螺仪姿态数据的代码:

 //根据陀螺仪的四元数转换为矩阵
+ (GLKMatrix4)calculateMatrixFromQuaternion:(CMQuaternion*)quaternion orientation:(AVCaptureVideoOrientation) orientation{
float xx = quaternion->x * quaternion->x;
float yy = quaternion->y * quaternion->y;
float zz = quaternion->z * quaternion->z;
float xy = quaternion->x * quaternion->y;
float wz = quaternion->w * quaternion->z;
float wy = quaternion->w * quaternion->y;
float xz = quaternion->x * quaternion->z;
float yz = quaternion->y * quaternion->z;
float wx = quaternion->w * quaternion->x; float r00 = - * (yy + zz);
float r01 = * (xy - wz);
float r02 = * (wy + xz);
float r03 = ; float r10 = * (xy + wz);
float r11 = - * (xx + zz);
float r12 = * (yz - wx);
float r13 = ; float r20 = * (xz - wy);//xy - wy
float r21 = * (yz + wx);
float r22 = - * (xx + yy);
float r23 = ; float r30 = ;
float r31 = ;
float r32 = ;
float r33 = ; return GLKMatrix4Make(r00,r01,r02,r03,
r10,r11,r12,r13,
r20,r21,r22,r23,
r30,r31,r32,r33);
} //当前屏幕的方向
@property (nonatomic, readwrite, assign) AVCaptureVideoOrientation orientation;
//开启陀螺仪
- (void)updateAngleFromAttitudeSensor{
if(self.cmmotionManager){ }else{
self.cmmotionManager = [[CMMotionManager alloc] init];
self.cmmotionManager.deviceMotionUpdateInterval = 1.0/25.0;
self.cmmotionManager.gyroUpdateInterval = 1.0/25.0;
self.cmmotionManager.showsDeviceMovementDisplay = YES; NSOperationQueue *mQueue = [[NSOperationQueue alloc]init];
__weak typeof(self) wSelf = self;
[wSelf.cmmotionManager startDeviceMotionUpdatesToQueue:mQueue withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error){
CMAttitude *attitude = motion.attitude;
if (attitude == nil) {
return;
} //UID
CMQuaternion quaternion = attitude.quaternion;
wSelf.sensorMatrix = GLKMatrix4Identity;
wSelf.sensorMatrix = [GyroscopeUtil calculateMatrixFromQuaternion:&quaternion orientation:orientation]; wSelf.sensorMatrix = GLKMatrix4RotateX(wSelf.sensorMatrix,M_PI_2);
wSelf.sensorInverseMatrix = GLKMatrix4Identity; bool isInvertible = true;
wSelf.sensorInverseMatrix = GLKMatrix4Invert(wSelf.sensorMatrix, &isInvertible);
}];
}
}

iOS平台使用陀螺仪传感器的更多相关文章

  1. iOS开发——高级篇——传感器(加速计、摇一摇、计步器)

    一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...

  2. 【android】移植IOS视图响应陀螺仪交互行为

    IOS有个很有趣味的特性:背景图片可以响应陀螺仪方向的变化,去改变X.Y轴上的值,从而让整个界面看着充满着灵性.具体步骤是:解锁苹果产品,在IOS7以上,摆动手势,观察桌面背景图片的变化. 刚好,我们 ...

  3. 通过OpenGL ES在iOS平台实践增强现实

    http://www.cnblogs.com/elvisyzhao/p/3398250.html 本文采用OpenGL ES 1固定渲染管线实现,目标为在设备拍摄到的现实世界中,绘制世界坐标轴,并根据 ...

  4. 通过OpenGL ES在iOS平台实践增强现实(一)

    http://ios.9tech.cn/news/2013/1108/38495.html 1.本文采用OpenGL ES 1固定渲染管线实现,目标为在设备拍摄到的现实世界中,绘制世界坐标轴,并根据设 ...

  5. iOS平台快速发布HT for Web拓扑图应用

    iOS平台一直是封闭的生态圈,iOS开发者要缴纳年费加入开发者计划才可进行iOS平台的APP开发测试,所开发的APP需要上传到App Store经过苹果审核以后才可对外发布.如果要开发企业内部应用,则 ...

  6. (译)cocos2d-x跨android&ios平台开发入门教程

    免责申明(必读!):本博客提供的所有教程的翻译原稿均来自于互联网,仅供学习交流之用,切勿进行商业传播.同时,转载时不要移除本申明.如产生任何纠纷,均与本博客所有人.发表该翻译稿之人无任何关系.谢谢合作 ...

  7. [原创]自定义控件之AndroidSegmentControlView,仿IOS平台UISegmentControlView,继承自View

    版权声明:本文为博主原创文章,转载请注明出处. 控件简介 UISegmentControl在IOS平台的App中非常常见,其控件如下图所示: 这种控件的主要作用是动态的更改界面的显示内容,一般应用于内 ...

  8. [转]ios平台内存常见问题

    本文转自CocoaChina,说的满详细的: 链接地址:http://www.cocoachina.com/bbs/read.php?tid=94017&keyword=%C4%DA%B4%E ...

  9. U3D中IOS平台泛型方法尽少使用

    U3D的IOS最小运行库use micro mscorlib是不包含泛型反射方法的,如FieldType.GetGenericArguments方法.所以尽量少用List而直接使用array. 另外请 ...

随机推荐

  1. Flat UI 工具包

    Flat UI是一套精美的扁平风格 UI 工具包,基于 Twitter Bootstrap 实现.这套界面工具包含许多基本的和复杂的 UI 部件,例如按钮,输入框,组合按钮,复选框,单选按钮,标签,菜 ...

  2. 在skyDriver上保存代码

    在家里写的代码, 有时候在公司也想打开改一改. 以前, 我都是使用优盘进行拷贝, 或者直接在优盘上进行操作. 有时, 忘了带优盘就傻眼了. 也想过直接托管到代码托管网站. 但主流的一些托管,都是要开源 ...

  3. Nginx和Apache配置日志格式记录Cookie

    记录Cookie有什么用? 有时候我们需要通过web服务器的访问日志来统计UV(独立访客),并据此分析用户的行为.而UV是依据cookie数据得出的统计.UV相对于IP的好处是:IP是一个反映网络虚拟 ...

  4. js post提交

    /* js跳转页面 url跳转地址,params参数列表 */ function Posturl(url,params){ //创建form表单 var temp_form=document.crea ...

  5. maven的阿里镜像

    偶然发现maven有了阿里镜像 vim ~/.m2/setting.xml <mirrors> <mirror> <id>alimaven</id> & ...

  6. 跨平台开发之阿里Weex框架环境搭建(一)

    转载自:http://www.cnblogs.com/fozero/p/5995122.html 一.介绍 Weex是阿里今年6月份推出的跨平台解决方案,6月底正式开源.官网 https://alib ...

  7. AMD高级应用(翻译)

    Dojo now supports modules written in the Asynchronous Module Definition (AMD) format, which makes co ...

  8. 08void

    void void修饰函数返回值和参数 如果函数没有返回值,那么应给将其声明为void 如果函数没有参数,应该声明其参数为void 不存在void类型的变量 void指针的意义 C语言规定只有相同类型 ...

  9. [asp.net]c# winform打印类

    using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using ...

  10. NPOIHelper

    public class NPOIHelper { public static void WriteDataToExceel(string fileName, DataSet ds) { if (Fi ...