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-10-24 21:23:08