Ios (ipad, iphone) general method of screen rotation Detection
In special scenarios, screen rotation is needed. It is convenient to implement related functions in ios.
Here are two methods:
1. register the UIApplicationDidChangeStatusBarOrientationNotification notification (for example, register the notification in viewdidload of a viewcontroller class). The sample code is as follows:
[[Nsnotifcencenterdefacenter] addObserver: selfselector: @ selector (statusBarOrientationChange :) name: uiapplicationdidchangestatusbarorientationicationicationobject: nil];
-(Void) statusBarOrientationChange :( NSNotification *) notification
{
UIInterfaceOrientation orientation = [[UIApplicationsharedApplication] statusBarOrientation];
If (orientation = UIInterfaceOrientationLandscapeRight) // right-click the home Key
{
//
}
If (
Orientation = UIInterfaceOrientationLandscapeLeft) // press the home Key to the left
{
//
}
If (orientation = UIInterfaceOrientationPortrait)
{
//
}
If (orientation = UIInterfaceOrientationPortraitUpsideDown)
{
//
}
}
Note that this method listens to StatusBar, that is, the direction of the status bar. Therefore, it is related to your layout. If your layout is changed, you will receive this notification, not the notification of device rotation.
When we focus on things related to Layout rather than purely device rotation, we use the above Code as an implementation solution.
2. register the UIDeviceOrientationDidChangeNotification notification (for example, we also register the notification in viewdidload of the viewcontroller class). The sample code is as follows:
[[Nsicationcenter center defacenter center] addObserver: selfselector: @ 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;
}
}
Note that the direction in this method also includes face-up or face-down. It is easy to see that this is based entirely on the physical direction of the device itself. When we focus only on the physical orientation, we usually need to register the notification to solve the problem (there is also an api for the accelerator, which can implement similar functions, the api is lower-layer, we recommend that you do not use the above two methods to solve the problem. Improper use causes high performance loss ).