Orientation - 3

来源:互联网 发布:非线性最优化方法 编辑:程序博客网 时间:2024/06/05 16:47

下面用在旋转视图控制器的几个方法,非常好用,其实还有大量的其他判断方法,都不太靠谱


#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


0 0