iOS平台使用陀螺仪传感器
在移动端开发过程中,有时候会用到陀螺仪传感器获取当前手机的姿态,下面给出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平台使用陀螺仪传感器的更多相关文章
- iOS开发——高级篇——传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- 【android】移植IOS视图响应陀螺仪交互行为
IOS有个很有趣味的特性:背景图片可以响应陀螺仪方向的变化,去改变X.Y轴上的值,从而让整个界面看着充满着灵性.具体步骤是:解锁苹果产品,在IOS7以上,摆动手势,观察桌面背景图片的变化. 刚好,我们 ...
- 通过OpenGL ES在iOS平台实践增强现实
http://www.cnblogs.com/elvisyzhao/p/3398250.html 本文采用OpenGL ES 1固定渲染管线实现,目标为在设备拍摄到的现实世界中,绘制世界坐标轴,并根据 ...
- 通过OpenGL ES在iOS平台实践增强现实(一)
http://ios.9tech.cn/news/2013/1108/38495.html 1.本文采用OpenGL ES 1固定渲染管线实现,目标为在设备拍摄到的现实世界中,绘制世界坐标轴,并根据设 ...
- iOS平台快速发布HT for Web拓扑图应用
iOS平台一直是封闭的生态圈,iOS开发者要缴纳年费加入开发者计划才可进行iOS平台的APP开发测试,所开发的APP需要上传到App Store经过苹果审核以后才可对外发布.如果要开发企业内部应用,则 ...
- (译)cocos2d-x跨android&ios平台开发入门教程
免责申明(必读!):本博客提供的所有教程的翻译原稿均来自于互联网,仅供学习交流之用,切勿进行商业传播.同时,转载时不要移除本申明.如产生任何纠纷,均与本博客所有人.发表该翻译稿之人无任何关系.谢谢合作 ...
- [原创]自定义控件之AndroidSegmentControlView,仿IOS平台UISegmentControlView,继承自View
版权声明:本文为博主原创文章,转载请注明出处. 控件简介 UISegmentControl在IOS平台的App中非常常见,其控件如下图所示: 这种控件的主要作用是动态的更改界面的显示内容,一般应用于内 ...
- [转]ios平台内存常见问题
本文转自CocoaChina,说的满详细的: 链接地址:http://www.cocoachina.com/bbs/read.php?tid=94017&keyword=%C4%DA%B4%E ...
- U3D中IOS平台泛型方法尽少使用
U3D的IOS最小运行库use micro mscorlib是不包含泛型反射方法的,如FieldType.GetGenericArguments方法.所以尽量少用List而直接使用array. 另外请 ...
随机推荐
- kaungbin_DP S (POJ 3666) Making the Grade
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- android NDK debug 遇到的问题与解决方法
最近在研究android NDK 的eclipse调试,遇到点问题,总结一下: 1.Unknown Application ABI :在application.mk里面添加APP_PLATFORM ...
- 编写一个Singleton程序(单例)
public class Test { private static Test test = new Test(); private Test(){}//构造方法私有化 private static ...
- unity, 按类型查找文件
- jquery筛选器
1.过滤 eq(index|-index) 获取当前链式操作中第N个jquery对象,正数从0开始,负数从-1开始. first 获取当前链式操作中第1个jquery对象 last 获取当前链式 ...
- jQuery中,$('#main') 与 document.getElementById('main')是什么样的关系-转
$('#main')[0]和document.getElementById('main')两个一模一样.解释:$('#main'):是一个jquery写法,#main是一个过滤器表示方法,表示查找一个 ...
- PHP list,explode的使用
PHP list,explode的使用 <?php header("Content-type: text/html; charset=utf-8"); echo " ...
- winform 异步添加文本提示
后台代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...
- [2015.08.13]万峰快递单打印软件 v2.2
<万峰快递单打印软件>是一款简单易用,且支持所有快递单的打印软件.1.支持文字打印位置的坐标精确定位和微调的打印工具.2.操作灵活,只需录入收件人和发件人信息即可打印:3.快递单打印尺寸和 ...
- Mapcontrol 遍历所有图层方法
mapcontrol 遍历所有图层方法 2011-04-29 19:51 通过IMap中的get_layers()可以遍历MapControl中当前的图层.此方法可以通过指定UID对图层进行过滤或者分 ...