iOS Detect screen orientation

Source: Internet
Author: User
<span id="Label3"></p><p><p>Method One: Notification Center monitoring</p></p><pre>-(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) notifitionsatatus{<br>[[uidevice currentdevice] begingeneratingdeviceorientationnotifications]; [[nsnotificationcenter defaultcenter] addobserver:self selector: @selector (statusbarorientationchange:) name: Uiapplicationdidchangestatusbarorientationnotification</span></span><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: #0000ff;"><span style="color: #0000ff;">void</span></span>) Statusbarorientationchange: (nsnotification*<span style="color: #000000;"><span style="color: #000000;">) notification{uiinterfaceorientation Orientation</span></span>=<span style="color: #000000;"><span style="color: #000000;">[[uiapplication sharedapplication] statusbarorientation]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(orientation = = Uiinterfaceorientationlandscaperight)<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Home key on the right</span></span><span style="color: #000000;"><span style="color: #000000;"> { } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(orientation ==uiinterfaceorientationlandscapeleft)<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Home key on left</span></span><span style="color: #000000;"><span style="color: #000000;"> { } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(orientation = = Uiinterfaceorientationportrait)<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">home in the next</span></span><span style="color: #000000;"><span style="color: #000000;"> { } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(orientation = =<span style="color: #000000;"><span style="color: #000000;">Uiinterfaceorientationportraitupsidedown) {} NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%ld</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, orientation);}</span></span></pre><p><p>Note: the notification is invalid without turning on the screen rotation switch</p></p><p><p>Method Two: <span style="font-family: 微软雅黑; font-size: 14px;">Gyroscope</span> Monitoring</p></p><pre>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) startmotionmanager{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(_motionmanager = =<span style="color: #000000;"><span style="color: #000000;">Nil) {_motionmanager</span></span>=<span style="color: #000000;"><span style="color: #000000;">[[cmmotionmanager alloc] init]; }<br>  <span style="color: #008000;">//detection interval</span> _motionmanager.devicemotionupdateinterval</span></span>=<span style="color: #800080;"><span style="color: #800080;">1.0</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(_motionmanager.devicemotionavailable) {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Device Motion Available</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); [_motionmanager Startdevicemotionupdatestoqueue:[nsoperationqueue currentqueue] Withhandler:</span></span>^ (cmdevicemotion *motion, nserror *<span style="color: #000000;"><span style="color: #000000;">Error) {[self performselectoronmainthread: @selector (handledevicemotion:) withobje Ct:motion waituntildone:yes]; }]; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">No Device motion on Device.</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); [self setmotionmanager:nil]; }}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) handledevicemotion: (cmdevicemotion *<span style="color: #000000;"><span style="color: #000000;">) devicemotion{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Double</span></span>x =<span style="color: #000000;"><span style="color: #000000;">devicemotion.gravity.x; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Double</span></span>y =<span style="color: #000000;"><span style="color: #000000;">devicemotion.gravity.y; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(fabs (y) >=<span style="color: #000000;"><span style="color: #000000;">fabs (x)) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(y >=<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">){ </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">uideviceorientationportraitupsidedown;</span></span>[self.<span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span><span style="color: #000000;"><span style="color: #000000;">actionwithdeviceorientation:uideviceorientationportraitupsidedown]; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{ </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">uideviceorientationportrait;</span></span>[self.<span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span><span style="color: #000000;"><span style="color: #000000;">actionwithdeviceorientation:uideviceorientationportrait]; } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(x >=<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">){ </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">uideviceorientationlandscaperight;</span></span>[self.<span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span><span style="color: #000000;"><span style="color: #000000;">actionwithdeviceorientation:uideviceorientationlandscaperight]; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{ </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">uideviceorientationlandscapeleft;</span></span>[self.<span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span><span style="color: #000000;"><span style="color: #000000;">actionwithdeviceorientation:uideviceorientationlandscapeleft]; } }}<br><span style="color: #008000;">//stop Detection</span> </span></span>-(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Endmotionmanager{[_motionmanager stopdevicemotionupdates];}</span></span></pre><p><p>iOS Detect screen orientation</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.