下面用在旋转视图控制器的几个方法,非常好用,其实还有大量的其他判断方法,都不太靠谱
#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
新闻热点
疑难解答