iOS-横竖屏基础

一、基础知识

1、初识3个枚举

1.1、UIDevice.h文件中:设备方向

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    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
} API_UNAVAILABLE(tvos);

1.2、UIApplication.h文件中:界面方向

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} API_UNAVAILABLE(tvos);

1.3、UIApplication.h文件中:界面方向掩码

typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
} API_UNAVAILABLE(tvos);

2、3个枚举之间的关系

2.1、UIDeviceOrientation和UIInterfaceOrientation有相同意义的枚举
2.2、UIInterfaceOrientationMask是通过UIInterfaceOrientation的不同值取或运算

3、获取横竖屏状态的方法

3.1、获取UIDeviceOrientation

UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;

3.2、获取UIInterfaceOrientation

UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

二、原理

1、常见的UI 架构:

image.png
我们是某些界面需要横屏,所以必须要把系统的询问细化到每个控制器的方法才行。
image.png

2、结合上图,我们看下一个横竖屏事件的传递过程:

  • 先是捕获到一个横屏事件
  • 接下来系统会找到当前用户操作的那个 APP
  • APP 会找到当前的窗口 window
  • 窗口 window 会找到根控制器,这个时候事件终于传到我们开发者手里了
  • 对于我们自定义的根控制器,它需要把这个事件传递到 UITabbarController
  • 对于 UITabbarController,需要把事件传递到 UINavigationController
  • 对于 UINavigationController,需要把事件传递到我们自己的控制器
  • 最后在我们自己的控制器中决定某个界面是否需要横屏

3、现象

3.1.push ViewController的时候:只有才会触发(如果一开始就是横屏是不会去调用shouldAutorotate方法)
3.2.present ViewController的时候:都会去调用shouldAutorotate方法

三、实战应用

1、应用场景

1.1.全应用都是竖屏

image.png

1.2.大部分页面都是竖屏,只有个别页面是可以横竖屏切换(eg.视频播放页面)

// 1.在appDelegate中重写
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)aWindow {
    UIInterfaceOrientationMask mask = [self.viewController.currentTopViewController supportedInterfaceOrientations];
    return mask;
}
// 2、在ViewController的基类中实现统一竖屏(只能竖屏的ViewController就继承该基类)
#pragma mark - rotation

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

// default value is YES
- (BOOL)shouldAutorotate {
    return NO;
}

// default value
// iPhone: UIInterfaceOrientationMaskPortraitUpsideDown
// iPad: UIInterfaceOrientationMaskAll
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController {
    return UIInterfaceOrientationPortrait;
}
// 3、在需要能够横竖屏切换的界面重写方法
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (BOOL)shouldAutorotate {
    return YES;
}

注意:我们的项目中可能又成千上万个控制器,如果每个控制器都实现是否横竖屏转动的方法显然不大实际。

推荐↓

1.在UIViewController的或者中实现即可(例如:统一不能旋转横竖屏)
2.特殊可以横竖屏切换的页面,在ViewController重写方法(例如:视频播放页面能旋转横竖屏)

1.3.大部分都是可以横竖屏切换,只有少部分页面固定(跟1.2相反)

2、手动横竖屏方法(亲测能过包)

// 手动横屏
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];
    [UIViewController attemptRotationToDeviceOrientation];
// 手动竖屏
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
    [UIViewController attemptRotationToDeviceOrientation];
// call this method when your return value from shouldAutorotateToInterfaceOrientation: changes
// if the current interface orientation does not match the current device orientation, a rotation may occur provided all relevant view controllers now return YES from shouldAutorotateToInterfaceOrientation:
+ (void)attemptRotationToDeviceOrientation API_AVAILABLE(ios(5.0)) API_UNAVAILABLE(tvos);

你可能感兴趣的:(iOS-横竖屏基础)