下面用在旋轉視圖控制器的幾個方法,非常好用,其實還有大量的其他判斷方法,都不太靠譜
#import "OrientationViewController.h"
@interface OrientationViewController ()
@property (nonatomic, assign) BOOL shouldRotate;
@end
@implementation OrientationViewController
- (void)viewDidLoad
{
[super viewDidLoad];
/* iOS7 視圖進入的時候判斷方向 */
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8)
{
switch ([UIDevice currentDevice].orientation)
{
case UIDeviceOrientationPortrait:
{
NSLog(@"Portrait !!!");
break;
}
case UIDeviceOrientationLandscapeLeft:
case UIDeviceOrientationLandscapeRight:
{
NSLog(@"Landspace !!!");
break;
}
default:
NSLog(@"Unkown !!!");
break;
}
}
}
- (void)configureNotificaiton
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:)
name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
- (void)statusBarOrientationChange:(NSNotification *)notification
{
/* 配合這個方法,用在更新界面,控制先後 */
}
#pragma mark - UIViewController (UIViewControllerRotation).
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8)
{
switch ([UIDevice currentDevice].orientation)
{
case UIDeviceOrientationPortrait:
{
NSLog(@"Portrait !!!");
break;
}
case UIDeviceOrientationLandscapeLeft:
case UIDeviceOrientationLandscapeRight:
{
NSLog(@"Landspace !!!");
break;
}
default:
NSLog(@"Unkown !!!");
break;
}
}
else
{
/* iOS8 以上的處理 */
}
}
#pragma mark - Orientation Handler.
- (BOOL)shouldAutorotate
{
if (self.shouldRotate)
return YES;
else
return NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if (self.shouldRotate)
return YES;
else
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
if (!self.shouldRotate)
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
return UIInterfaceOrientationMaskAll;
}
@end