iOS (Ipad,iphone) screen rotation detection general method

Source: Internet
Author: User

In a special scenario, special handling is required for screen rotation. It is also convenient to implement the relevant functions under the iOS system.

Here are two ways to do this:

1. Register Uiapplicationdidchangestatusbarorientationnotification Notification (example: registering the notification in a viewdidload of a Viewcontroller Class), the sample code is as follows:

[[nsnotificationcenterdefaultcenter] addobserver:self selector: @selector (statusbarorientationchange:) name :uiapplicationdidchangestatusbarorientationnotificationobject: nil];

-(void) Statusbarorientationchange: (nsnotification *) notification

{

uiinterfaceorientation orientation = [[uiapplication Sharedapplication] statusbarorientation];

If (orientation = =uiinterfaceorientationlandscaperight) //Home key on the right

{

//

}

if (

Orientation = = Uiinterfaceorientationlandscapeleft)//home key on left

{

//

}

If (orientation = =uiinterfaceorientationportrait)

{

//

}


If (orientation = =Uiinterfaceorientationportraitupsidedown)

{

//

}

}

Note that this way of listening is StatusBar is the direction of the status bar, so this is related to your layout, your layout to go, will receive this notification, not the device rotation notification.

When we focus on things that are related to layout rather than purely device rotation, we use the above code as a solution for the implementation.

2. Registration Uideviceorientationdidchangenotification Notice (example: We also register the notification in a Viewcontroller class Viewdidload), the sample code is as follows:

[[nsnotificationcenter defaultcenter] addobserver:self selector: @selector (orientchange:) Name: uideviceorientationdidchangenotificationobject:nil];

-(void) Orientchange: (nsnotification *) Noti

{

nsdictionary* ntfdict = [Noti userInfo];

uideviceorientation orient = [uidevicecurrentdevice]. Orientation;

/*

Uideviceorientationunknown,

Uideviceorientationportrait,//Device oriented vertically, home button on the bottom

Uideviceorientationportraitupsidedown,//Device oriented vertically, home button on the top

Uideviceorientationlandscapeleft,//Device oriented horizontally, home button on the right

Uideviceorientationlandscaperight,//Device oriented horizontally, home button on the left

Uideviceorientationfaceup,//Device oriented flat, face up

Uideviceorientationfacedown//Device oriented flat, face down */

Switch (Orient)

{

caseuideviceorientationportrait:

break;

caseuideviceorientationlandscapeleft:

break;

caseuideviceorientationportraitupsidedown:

break;

caseuideviceorientationlandscaperight:

break;

default:

break;

}

}

Notice that the direction in this way also includes facing up or down, it is easy to see that this is entirely based on the physical direction of the device itself, when we focus on the physical orientation, we usually need to register the notification to solve the problem (there is also an accelerometer API, you can implement a similar function, the API is lower, In the case of the above two methods to solve the problem is not recommended, the use of improper performance loss is very large).




iOS (Ipad,iphone) screen rotation detection general method

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.