BBAOrientationManager.h 4.44 KB
//
//  BBAOrientationManager.h
//  rotation
//
//  Created by liuchengqing01 on 2017/9/24.
//  Copyright © 2017年 baidu. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "UIWindow+BBAInterfaceOrientations.h"

NS_ASSUME_NONNULL_BEGIN

// 强制转屏判断结果
typedef enum : NSUInteger {
    BBAForceOrientationCheckResultYes,   //  转屏
    BBAForceOrientationCheckResultNo,    //  不转屏
    BBAForceOrientationCheckResultUndefined,    //  不确定
} BBAForceOrientationCheckResult;

@interface BBAOrientationManager : NSObject

+ (instancetype _Nullable)shareInstance;

#pragma mark - auto rotate controller
/*
 横竖屏状态更新规则:
 1. vc不支持当前设备的横竖屏状态, 更新到偏好横竖屏状态
 2. vc支持当前设备的横竖屏状态, 但当前横竖屏状态与设备的横竖屏状态不一致, 更新到设备的横竖屏状态
 3. vc支持当前设备的横竖屏状态, 且当前横竖屏状态与设备的横竖屏状态一致, 无需进行更新
 4. vc不支持当前横竖屏状态, 立即更新状态
 5. vc支持当前横竖屏状态, 待调用updateOrientationIfNeeded时更新状态
 */
/**
 给即将展示的vc设置转向更新请求 (默认有动画)
 */
- (void)setNeedUpdateOrientationAdaptToViewController:(UIViewController *_Nullable)viewcontroler;
- (void)setNeedUpdateOrientationAdaptToViewController:(UIViewController *_Nullable)viewControler animation:(BOOL)animation;
- (void)setNeedUpdateOrientationAdaptToViewController:(UIViewController *_Nullable)viewControler window:(UIWindow *)window animation:(BOOL)animation;

/**
 清除转向更新请求
 */
- (void)clearUpdateOrientation;

/**
 跳过下一次转向更新(即updateOrientationIfNeeded),如PopFromLandscapeToPortraitOrientation与pushRootViewcontroller
 */
- (void)skipNextUpdateOrientation;

/**
 转向更新, 无转向请求直接跳过
 */
- (void)updateOrientationIfNeeded;

/**
 强制旋屏(默认app主window)
 */
- (void)forceRotateToOrientation:(UIInterfaceOrientation)orientation animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion;

/**
 强制旋屏(指定window)
 */
- (void)forceRotateWindow:(UIWindow *_Nonnull)window toOrientation:(UIInterfaceOrientation)orientation animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion;

/**
 检查并强制旋屏(指定window)
 备注:如果当前window与目标方向一致,立即执行回调
 */
- (void)checkAndForceRotateWindow:(UIWindow *_Nonnull)window toOrientation:(UIInterfaceOrientation)orientation animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion;

#pragma mark - window rotate controller
/*
 自定义window旋转屏控制:
 1.createOrientationManagerViewController作为window的RootViewcontroller
 2.自定义window的RootViewcontroller, 使用Manager中提供shouldAutorotate与supportedInterfaceOrientations作为RootViewcontroller的旋转控制
 3.自定义window的RootViewcontroller, 自己控制旋转
 */
/**
 需要自动转屏
 */
@property (nonatomic, assign, readonly) BOOL needAutorotate;

/**
 正在更新的横竖屏状态
 */
@property (nonatomic, assign, readonly) UIInterfaceOrientation adaptingOrientation;

/**
 横竖屏状态控制主容器
 */
@property (nonatomic, weak, readonly) UIViewController *rootViewController;

/**
 手势转场时转屏控制
 */
@property (nonatomic, assign) BOOL cannotRotate;

/**
 设置控制旋转的主vc容器, 需KeyWindow的rootViewController
 */
- (void)setupRootViewController:(UIViewController *_Nullable)rootViewController;

/**
 是否可以旋转, 由rootViewController控制
 */
- (BOOL)shouldAutorotate;

/**
 旋转方向支持, 由rootViewController控制
 */
- (UIInterfaceOrientationMask)supportedInterfaceOrientations;

/// 起始方向, 由rootViewController控制
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;

/**
 控制旋转的vc, 用于作为自定义window的RootViewcontroller
 */
- (UIViewController *_Nullable)createOrientationManagerViewController;

/**
 保持住对应横竖屏状态(慎用, 使用后会使系统转屏停留在固定的状态, forceRotateToOrientation无效, 该函数不会触发转屏动作, hold的状态需与当前的状态一致)
 */
- (void)holdOrientation:(UIInterfaceOrientation)orientation;

/**
 解除保持横竖屏状态(需与holdOrientation成对使用)
 */
- (void)cancelHoldOrientation;

/**
 强制转屏检查
 */
- (BBAForceOrientationCheckResult)checkForceAutorotate;

@end

NS_ASSUME_NONNULL_END