iOS development--practical technical article Swift & Accelerometer and Gyroscope

Source: Internet
Author: User
<span id="Label3"></p><p style="text-align: center;"><p style="text-align: center;"><span style="font-size: 18pt;">Accelerometers and gyroscopes</span></p></p><p><p></p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">return button Events</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #000000;"><span style="color: #000000;">@IBAction func backbuttonclick ()</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span>Self.navigationcontroller?. Popviewcontrolleranimated (<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008080;"><span style="color: #008080;">8</span></span>@IBOutlet var xlabel:uilabel!<span style="color: #008080;"><span style="color: #008080;">9</span></span>@IBOutlet var ylabel:uilabel!<span style="color: #008080;"><span style="color: #008080;">Ten</span></span>@IBOutlet var zlabel:uilabel!<span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #008080;"><span style="color: #008080;"></span> a</span>@IBOutlet var orientationlabel:uilabel!<span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Accelerometer Manager-all operations will be taken over by this motionmanager.</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>var motionmanager:cmmotionmanager!<span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Override</span></span><span style="color: #000000;"><span style="color: #000000;">func viewdidload () {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">super.viewdidload ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span>Titlelabel.text =<span style="color: #000000;"><span style="color: #000000;">titlestring</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008080;"><span style="color: #008080;"></span> at</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">------coremotion Accelerometer</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Motionmanager = Cmmotionmanager ()<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Note that Cmmotionmanager is not a single case</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Motionmanager.accelerometerupdateinterval =<span style="color: #800080;"><span style="color: #800080;">0.1</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set Read time interval</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>Motionmanager.accelerometeravailable<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">determine if an accelerometer can be used</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get the main thread concurrent queue, with the new UI in the main</span> thread</span><span style="color: #008080;"><span style="color: #008080;"></span> to</span>Motionmanager.startaccelerometerupdatestoqueue (nsoperationqueue.mainqueue (), withhandler: {(var accelerometerData : cmaccelerometerdata, var error:nserror?) -Void<span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>error! =<span style="color: #000000;"><span style="color: #000000;">Nil</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> *</span>Self.motionManager.stopAccelerometerUpdates ()<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">stop using the accelerometer</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span>}<span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #008080;"><span style="color: #008080;">Panax Notoginseng</span></span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span>Self.xLabel.text =<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">x:\ (accelerometerdata!. Acceleration.x)</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>Self.yLabel.text =<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">y:\ (accelerometerdata!. Acceleration.y)</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span>Self.zLabel.text =<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">z:\ (accelerometerdata!. Acceleration.z)</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;"> })</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008080;"><span style="color: #008080;"></span> $</span>}<span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Let Aler = Uialertview (title:<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">your device does not support the</span> accelerometer</span><span style="color: #800000;"><span style="color: #800000;">"</span></span>, message:nil,<span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span>: nil, cancelbuttontitle:<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">OK</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">aler.show ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;">Wuyi</span></span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> wu</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Sensing device Orientation-turn on monitoring device orientation</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">uidevice.currentdevice (). begingeneratingdeviceorientationnotifications ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> about</span> <span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">add notifications to monitor device orientation changes</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Nsnotificationcenter.defaultcenter (). addobserver (self, selector:<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">receivedrotation</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>, name:uideviceorientationdidchangenotification,<span style="color: #0000ff;"><span style="color: #0000ff;">Object</span></span><span style="color: #000000;"><span style="color: #000000;">: Nil)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Turn off monitoring device orientation</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;">uidevice.currentdevice (). endgeneratingdeviceorientationnotifications ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Override</span></span><span style="color: #000000;"><span style="color: #000000;">func didreceivememorywarning () {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #000000;"><span style="color: #000000;">super.didreceivememorywarning ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Dispose of any resources the can be Recreated.</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">MARK:-judging Device Direction Agent method</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #000000;"><span style="color: #000000;">func receivedrotation ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>var device =<span style="color: #000000;"><span style="color: #000000;">Uidevice.currentdevice ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> about</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>Device.orientation = =<span style="color: #000000;"><span style="color: #000000;">Uideviceorientation.unknown</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>Orientationlabel.text =<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Unknown</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>Device.orientation = =<span style="color: #000000;"><span style="color: #000000;">uideviceorientation.portrait</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;">Bayi</span></span>Orientationlabel.text =<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Portrait</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>Device.orientation = =<span style="color: #000000;"><span style="color: #000000;">Uideviceorientation.portraitupsidedown</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Orientationlabel.text =<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Portraitupsidedown</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>Device.orientation = =<span style="color: #000000;"><span style="color: #000000;">Uideviceorientation.landscapeleft</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>Orientationlabel.text =<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Landscapeleft</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>Device.orientation = =<span style="color: #000000;"><span style="color: #000000;">Uideviceorientation.landscaperight</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>Orientationlabel.text =<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Landscaperight</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #008080;"><span style="color: #008080;">94</span></span>}<span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>Device.orientation = =<span style="color: #000000;"><span style="color: #000000;">Uideviceorientation.faceup</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>Orientationlabel.text =<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Faceup</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;">98</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>Device.orientation = =<span style="color: #000000;"><span style="color: #000000;">Uideviceorientation.facedown</span></span><span style="color: #008080;"><span style="color: #008080;"></span> about</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Orientationlabel.text =<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">facedown</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #008080;"><span style="color: #008080;">101</span></span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;">102</span></span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;">103</span></span> <span style="color: #008080;"><span style="color: #008080;">104</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">MARK:-shaking Events</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Override</span></span>Func Motionbegan (motion:uieventsubtype, withevent<span style="color: #0000ff;"><span style="color: #0000ff;">Event</span></span><span style="color: #000000;"><span style="color: #000000;">: Uievent) {</span></span><span style="color: #008080;"><span style="color: #008080;">106</span></span> <span style="color: #008080;"><span style="color: #008080;">107</span></span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Motionbegan</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>)<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Start Shaking</span></span><span style="color: #008080;"><span style="color: #008080;">108</span></span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;">109</span></span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Override</span></span>Func motionended (motion:uieventsubtype, withevent<span style="color: #0000ff;"><span style="color: #0000ff;">Event</span></span><span style="color: #000000;"><span style="color: #000000;">: Uievent) {</span></span><span style="color: #008080;"><span style="color: #008080;">111</span></span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">motionended</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>)<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Shaking End</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;">113</span></span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Override</span></span>Func motioncancelled (motion:uieventsubtype, withevent<span style="color: #0000ff;"><span style="color: #0000ff;">Event</span></span><span style="color: #000000;"><span style="color: #000000;">: Uievent) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">motioncancelled</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>)<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">shaking was terminated unexpectedly</span></span><span style="color: #008080;"><span style="color: #008080;">117</span></span>}</pre><p><p></p></p><p><p>iOS development--practical technical article Swift & Accelerometer and Gyroscope</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.