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