ios中的陀螺仪和加速计

ios设备中有的加速计可以测量出加速度和重力。陀螺仪可用于确定设备的方向与每条坐标轴之间的夹角,可用于读取描述设备围绕其轴的旋转的值。

首先在工程中添加CoreMotion.framework

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(180, 38, 26); "><span style="color: #ff7470">#import </span><UIKit/UIKit.h></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(180, 38, 26); "><span style="color: #ff7470">#import </span><CoreMotion/CoreMotion.h></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); min-height: 21px; ">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); "><span style="color: #ff892f">@interface</span> ViewController : <span style="color: #b0627a">UIViewController</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); min-height: 21px; ">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(255, 137, 47); ">@property<span style="color: #3081c7"> (</span>strong<span style="color: #3081c7">, </span>nonatomic<span style="color: #3081c7">) </span><span style="color: #b0627a">CMMotionManager</span><span style="color: #3081c7"> *motionManager;</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(255, 137, 47); ">@property<span style="color: #3081c7"> (</span>weak<span style="color: #3081c7">, </span>nonatomic<span style="color: #3081c7">) </span>IBOutlet<span style="color: #3081c7"> </span><span style="color: #b0627a">UILabel</span><span style="color: #3081c7"> *accelerometerLabel;</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(255, 137, 47); ">@property<span style="color: #3081c7"> (</span>weak<span style="color: #3081c7">, </span>nonatomic<span style="color: #3081c7">) </span>IBOutlet<span style="color: #3081c7"> </span><span style="color: #b0627a">UILabel</span><span style="color: #3081c7"> *gyroscopeLabel;</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); min-height: 21px; ">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(255, 137, 47); ">@end</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(255, 137, 47); ">
</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(180, 38, 26); "><span style="color: #ff7470">#import </span>"ViewController.h"</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); min-height: 21px; ">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(255, 147, 0); "><span style="color: #ff892f">@interface</span><span style="color: #3081c7"> </span>ViewController<span style="color: #3081c7"> ()</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); min-height: 21px; ">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(255, 137, 47); ">@end</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); min-height: 21px; ">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); "><span style="color: #ff892f">@implementation</span> ViewController</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); "><span style="color: #ff892f">@synthesize</span> motionManager;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); "><span style="color: #ff892f">@synthesize</span> accelerometerLabel;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); "><span style="color: #ff892f">@synthesize</span> gyroscopeLabel;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); min-height: 21px; ">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">- (<span style="color: #ff892f">void</span>)viewDidLoad</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(175, 162, 39); "><span style="color: #3081c7">    [</span><span style="color: #ff892f">super</span><span style="color: #3081c7"> </span>viewDidLoad<span style="color: #3081c7">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(176, 98, 122); "><span style="color: #3081c7"><span style="white-space:pre">	</span></span><span style="color: #ff892f">self</span><span style="color: #3081c7">.</span><span style="color: #3495af">motionManager</span><span style="color: #3081c7"> = [[</span>CMMotionManager<span style="color: #3081c7"> </span><span style="color: #afa227">alloc</span><span style="color: #3081c7">] </span><span style="color: #afa227">init</span><span style="color: #3081c7">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(176, 98, 122); "><span style="color: #3081c7">    </span>NSOperationQueue<span style="color: #3081c7"> *queue = [[</span>NSOperationQueue<span style="color: #3081c7"> </span><span style="color: #afa227">alloc</span><span style="color: #3081c7">] </span><span style="color: #afa227">init</span><span style="color: #3081c7">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(0, 143, 0); "><span style="color: #3081c7">    </span>//加速计</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(0, 173, 170); "><span style="color: #3081c7">    </span><span style="color: #ff892f">if</span><span style="color: #3081c7"> (</span><span style="color: #3495af">motionManager</span><span style="color: #3081c7">.</span>accelerometerAvailable<span style="color: #3081c7">) {</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(0, 173, 170); "><span style="color: #3081c7">        </span><span style="color: #3495af">motionManager</span><span style="color: #3081c7">.</span>accelerometerUpdateInterval<span style="color: #3081c7"> = </span><span style="color: #ff6666">1.0</span><span style="color: #3081c7">/</span><span style="color: #ff6666">10.0</span><span style="color: #3081c7">;</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">        [<span style="color: #3495af">motionManager</span> <span style="color: #afa227">startAccelerometerUpdatesToQueue</span>:queue <span style="color: #afa227">withHandler</span>:^(<span style="color: #b0627a">CMAccelerometerData</span> *accelerometerData,<span style="color: #b0627a">NSError</span> *error){</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">            <span style="color: #b0627a">NSString</span> *labelText;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">            <span style="color: #ff892f">if</span> (error) {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(175, 162, 39); "><span style="color: #3081c7">                [</span><span style="color: #3495af">motionManager</span><span style="color: #3081c7"> </span>stopAccelerometerUpdates<span style="color: #3081c7">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">                labelText = [<span style="color: #b0627a">NSString</span> <span style="color: #afa227">stringWithFormat</span>:<span style="color: #b4261a">@"Accelerometer encountered error: %@"</span>,error];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">            }<span style="color: #ff892f">else</span>{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">                labelText = [<span style="color: #b0627a">NSString</span> <span style="color: #afa227">stringWithFormat</span>:<span style="color: #b4261a">@"加速计\nx: %+.2f\ny: %+.2f\nz: %+.2f"</span>,accelerometerData.<span style="color: #00adaa">acceleration</span>.x,accelerometerData.<span style="color: #00adaa">acceleration</span>.y,accelerometerData.<span style="color: #00adaa">acceleration</span>.z];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">            }</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(175, 162, 39); "><span style="color: #3081c7">            [</span><span style="color: #3495af">accelerometerLabel</span><span style="color: #3081c7"> </span>performSelectorOnMainThread<span style="color: #3081c7">:</span><span style="color: #ff892f">@selector</span><span style="color: #3081c7">(setText:) </span>withObject<span style="color: #3081c7">:labelText </span>waitUntilDone<span style="color: #3081c7">:</span><span style="color: #ff892f">NO</span><span style="color: #3081c7">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">        }];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">    }<span style="color: #ff892f">else</span>{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(180, 38, 26); "><span style="color: #3081c7">        </span><span style="color: #3495af">accelerometerLabel</span><span style="color: #3081c7">.</span><span style="color: #00adaa">text</span><span style="color: #3081c7"> = </span>@"This device has no accelerometer."<span style="color: #3081c7">;</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">    }</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(0, 143, 0); "><span style="color: #3081c7">    </span>//陀螺仪</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(52, 149, 175); "><span style="color: #3081c7">    </span><span style="color: #ff892f">if</span><span style="color: #3081c7"> (</span>motionManager<span style="color: #3081c7">.</span><span style="color: #00adaa">gyroAvailable</span><span style="color: #3081c7">) {</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(0, 173, 170); "><span style="color: #3081c7">        </span><span style="color: #3495af">motionManager</span><span style="color: #3081c7">.</span>gyroUpdateInterval<span style="color: #3081c7"> = </span><span style="color: #ff6666">1.0</span><span style="color: #3081c7">/</span><span style="color: #ff6666">10.0</span><span style="color: #3081c7">;</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">        [<span style="color: #3495af">motionManager</span> <span style="color: #afa227">startGyroUpdatesToQueue</span>:queue <span style="color: #afa227">withHandler</span>:^(<span style="color: #b0627a">CMGyroData</span> *gyroData,<span style="color: #b0627a">NSError</span> *error){</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">            <span style="color: #b0627a">NSString</span> *labelText;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">            <span style="color: #ff892f">if</span> (error) {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">                [<span style="color: #3495af">motionManager</span> <span style="color: #afa227">stopGyroUpdates</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">                labelText = [<span style="color: #b0627a">NSString</span> <span style="color: #afa227">stringWithFormat</span>:<span style="color: #b4261a">@"Gyroscope encountered error: %@"</span>,error];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">            }<span style="color: #ff892f">else</span>{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">                labelText = [<span style="color: #b0627a">NSString</span> <span style="color: #afa227">stringWithFormat</span>:<span style="color: #b4261a">@"陀螺仪\nx: %+.2f\ny: %+.2f\nz: %+.2f"</span>,gyroData.<span style="color: #00adaa">rotationRate</span>.x,gyroData.<span style="color: #00adaa">rotationRate</span>.y,gyroData.<span style="color: #00adaa">rotationRate</span>.z];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">            }</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(175, 162, 39); "><span style="color: #3081c7">            [</span><span style="color: #3495af">gyroscopeLabel</span><span style="color: #3081c7"> </span>performSelectorOnMainThread<span style="color: #3081c7">:</span><span style="color: #ff892f">@selector</span><span style="color: #3081c7">(setText:) </span>withObject<span style="color: #3081c7">:labelText </span>waitUntilDone<span style="color: #3081c7">:</span><span style="color: #ff892f">NO</span><span style="color: #3081c7">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">        }];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">    }<span style="color: #ff892f">else</span>{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(180, 38, 26); "><span style="color: #3081c7">        </span><span style="color: #3495af">gyroscopeLabel</span><span style="color: #3081c7">.</span><span style="color: #00adaa">text</span><span style="color: #3081c7"> = </span>@"This device has no gyroscope"<span style="color: #3081c7">;</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">    }</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); min-height: 21px; ">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">- (<span style="color: #ff892f">void</span>)didReceiveMemoryWarning</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(175, 162, 39); "><span style="color: #3081c7">    [</span><span style="color: #ff892f">super</span><span style="color: #3081c7"> </span>didReceiveMemoryWarning<span style="color: #3081c7">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); ">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(48, 129, 199); min-height: 21px; ">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: FangSong; color: rgb(255, 137, 47); ">@end</p>

时间: 2024-08-25 20:40:38

ios中的陀螺仪和加速计的相关文章

iOS新加速计事件(陀螺仪和加速计)

iOS新加速计事件 [iOS新加速计事件] 1.iOS5.0以前,可以使用UIAcceleration来监听加速计事件. 2.Bug iOS5.0以后,UIAccelerometerDelegate已经被depreacated,如下: deprecated不是说不能说了,而是意味着在将来版本会删除,所以如果不想更新知识的话,就使用UIAccelerometer吧.更保险的方法是使用一个Timer来检查UIAcceleration,即不依赖于此Delegate回调. 3.针对iOS4.0以上版本,

陀螺仪以及三轴陀螺仪和六轴陀螺仪的区别_六轴陀螺仪和九轴陀螺仪的区别

来源:电子发烧友 链接:http://www.elecfans.com/article/88/142/2017/20171201590857.html 陀螺仪,是一种用来感测与维持方向的装置,基於角动量不灭的理论设计出来的.陀螺仪主要是由一个位於轴心可以旋转的轮子构成. 陀螺仪一旦开始旋转,由於轮子的角动量,陀螺仪有抗拒方向改变的趋向.陀螺仪多用於导航.定位等系统. 1850年法国的物理学家福柯(J.Foucault)为了研究地球自转,首先发现高速转动中的转子(rotor),由于惯性作用它的旋转

iOS中的事件处理

前言:iOS中事件处理,是一个很重要也很难得地方.涉及到响应者链的地方的面试题,很多工作两三年的老鸟也未必能回答的很专业.这里详细介绍一下iOS中的事件处理,以及响应者链. 1. 三大事件 触摸事件 加速计时间 远程控制事件 2. 响应者对象 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件.我们称之为 响应者对象 UIApplication.UIViewController.UIView都继承自UIResponder,因此它们都是响应者对象,都能够接

一、iOS中的事件可以分为3大类型

触摸事件加速计事件远程控制事件 响应者对象在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件.我们称之为"响应者对象" UIApplication.UIViewController.UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件 二.UIResponder UIResponder内部提供了以下方法来处理事件触摸事件- (void)touchesBegan:(NSSet *)touches withEve

iOS开发笔记--iOS中的触摸事件和手势处理

iOS中的事件可以分为三大类:原文:http://my.oschina.net/aofe/blog/268749 1> 触摸事件 2> 加速计事件 3> 远程控制事件 响应者对象 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件.我们称之为"响应者对象". UIApplication,UIViewController,UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件. UIRespon

iOS中的触摸事件

1.响应者对象 iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件,我们称之为“响应者对象”. UIApplication.UIViewController.UIView都继承自UIResponder,因此他们都是响应者对象,都能够接收并处理事件. 2.UIResponde UIResponder内部提供了以下方法来处理事件 触摸事件: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)e

ios中的事件处理、响应者链条以及第一响应者

在ios中,事件UIEvent类来表示,当一个事件发生时,系统会搜集的相关事件信息,创建一个UIEvent对象,最后将该事件转发给应用程序对象(UIApplication).日常生活中,主要有三种类型的事件:触摸事件,加速计事件以及远程遥控事件.下面是官方的一张图片: 当用户通过以上方式触发一个事件时,会将相应的事件对象添加到UIApplication的事件队列中.UIApplication会循环的从队列中拿出第一个事件来处理.首先将该事件分发给UIApplication 的主窗口对象(KeyW

1.0 iOS中的事件

在用户使用app过程中,会产生各种各样的事件,iOS中的事件可以分为3大类型: UIKit可识别三种类型的输入事件: 触摸事件 加速计事件 / 运动事件 远程控制事件 UIResponder - 响应者对象 概念: 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件. 成员: UIApplication.UIViewController.UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件 UIResponder的内部

iOS中的事件

iOS中的事件 在用户使用app过程中,会产生各种各样的事件 iOS中的事件可以分为3大类型 -- 触摸事件 ---- 加速计事件 ---- 远程控制事件- 响应者对象 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件.我们称之为"响应者对象" UIApplication.UIViewController.UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件 UIResponder UIResponder内