iOS之屏幕旋转(横屏),看我就够了

xiaoxiao2021-02-28  118

前言:

1.其实大多数app并不需要手动控制屏幕的旋转,甚至可能都是不允许旋转的,但是如果涉及到视频播放界面,那么想必一定会用到手动控制屏幕旋转,或者指定某个控制器能够旋转.

2.这里我就把对于屏幕旋转的所有情况的处理方法都列出出来,当然,重点是指定控制器旋转,并可手动控制

1.app不需要屏幕旋转

如图所示,只勾选Portrait即可.

或者在APPdelegate.m中实现如下方法

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskPortrait; }
2.app需要旋转
(1)跟随系统型,当手机解锁屏幕旋转时,app能够旋转,关闭屏幕旋转时app不能旋转

如下图所示即可 或者在APPdelegate.m中实现如下方法

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskAll; }

重点来了:

(2)随心所欲型,可指定某个控制器能够屏幕旋转,并可手动控制.

1.打开General,,设置如下 2.给APPdelegate添加BOOL类型属性

// APPdelegate.h 添加该属性 #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; // 判断是否允许屏幕旋转 @property (nonatomic,assign)BOOL allowRotation; @end // APPdelegate.m 实现如下方法 - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if (_allowRotation) { return UIInterfaceOrientationMaskAll; }else{ return UIInterfaceOrientationMaskPortrait; } }

3.在你需要旋转的控制器内导入AppDelegate.h文件

#import "AppDelegate.h" - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.allowRotation = NO; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.allowRotation = YES; } 此时已经实现指定控制器开启屏幕旋转,但此时仍是”跟随系统型”,也就是说还不能手动控制屏幕的旋转,当手机锁定屏幕旋转时,无法进行屏幕旋转.接下来便是实现手动控制,即使手机锁定屏幕旋转,仍能控制屏幕的旋转

4.手动控制横屏.创建一个按钮,点击该按钮实现屏幕的横屏与竖屏,暂且称该按钮为btn

// MARK: 点击btn按钮 - (void)fullButtonClick:(UIButton *)sender { // 这里我是通过按钮的selected状态来判定横屏竖屏的,并不是唯一的判断标准 if (sender.selected) { [self changeOrientation:UIInterfaceOrientationPortrait]; }else{ [self changeOrientation:UIInterfaceOrientationLandscapeRight]; } } /** * 强制横屏 * * @param orientation 横屏方向 */ - (void)changeOrientation:(UIInterfaceOrientation)orientation { int val = orientation; if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { SEL selector = NSSelectorFromString(@"setOrientation:"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:[UIDevice currentDevice]]; [invocation setArgument:&val atIndex:2]; [invocation invoke]; } } 至此,我们便实现了指定控制器旋转(横屏),并可手动控制.此时可能有同学会问,我需要屏幕旋转后进行其他处理,那么我应该在哪里处理,怎么处理呢?接下来,便是监听手机设备的屏幕旋转.

5.监听手机设备屏幕方向变化.

// 控制器的viewDidLoad方法 - (void)viewDidLoad { [super viewDidLoad]; // 接收屏幕方向改变通知,监听屏幕方向 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationHandler) name:UIDeviceOrientationDidChangeNotification object:nil]; [self setupUI]; } // 通知方法 - (void)orientationHandler { // 在这里可进行屏幕旋转时的处理 // 获取当前设备方向 UIDeviceOrientation orient = [UIDevice currentDevice].orientation; // 动画时长 NSTimeInterval duration = 0.3; // 获取宽高 CGFloat w = CGRectGetWidth(self.view.bounds); CGFloat h = CGRectGetHeight(self.view.bounds); // 处理方法,该方法参数根据跟人情况而定 [self fullScreenWithUIDeviceOrientation:orient duration:duration width:w height:h]; } // 处理横屏竖屏 - (void)fullScreenWithUIDeviceOrientation:(UIDeviceOrientation)orientation duration:(NSTimeInterval)duration width:(CGFloat)width height:(CGFloat)height { if (orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown || orientation == UIDeviceOrientationPortraitUpsideDown) return; if (orientation == UIDeviceOrientationPortrait) { // 竖屏 // 处理方法 }else{ // 向左旋转或向右旋转 // 处理方法 } } 补充:UIDeviceOrientationFaceUp = 手机正面向上.不管你手机横屏,还是竖屏,当手机屏幕向上时,获取的设备方向都是FaceUp UIDeviceOrientationFaceDown = 手机正面向下.也就是不管你手机横屏还是竖屏,当手机屏幕向下时,获取的设备方向都是FaceDown. UIDeviceOrientationPortraitUpsideDown = 竖屏且顶部旋转至home键方向,iPhone设备是不允许的,所以iPhone上不会出现这个参数.但是iPad上可以. 当屏幕与水平线垂直时,旋转时才会获取到UIDeviceOrientationPortrait,UIDeviceOrientationLandscapeLeft,UIDeviceOrientationLandscapeRight三个参数.
转载请注明原文地址: https://www.6miu.com/read-44808.html

最新回复(0)