美文网首页
【转】iOS开发:屏幕的旋转

【转】iOS开发:屏幕的旋转

作者: 棒棒德 | 来源:发表于2019-12-30 09:44 被阅读0次

Device Orientation:设备方向

typedefNS_ENUM(NSInteger,UIDeviceOrientation){UIDeviceOrientationUnknown,UIDeviceOrientationPortrait,// Device oriented vertically, home button on the bottomUIDeviceOrientationPortraitUpsideDown,// Device oriented vertically, home button on the topUIDeviceOrientationLandscapeLeft,// Device oriented horizontally, home button on the rightUIDeviceOrientationLandscapeRight,// Device oriented horizontally, home button on the leftUIDeviceOrientationFaceUp,// Device oriented flat, face upUIDeviceOrientationFaceDown// Device oriented flat, face down}

UIInterfaceOrientation:界面方向

typedefNS_ENUM(NSInteger,UIInterfaceOrientation){UIInterfaceOrientationUnknown=UIDeviceOrientationUnknown,UIInterfaceOrientationPortrait=UIDeviceOrientationPortrait,UIInterfaceOrientationPortraitUpsideDown=UIDeviceOrientationPortraitUpsideDown,UIInterfaceOrientationLandscapeLeft=UIDeviceOrientationLandscapeRight,UIInterfaceOrientationLandscapeRight=UIDeviceOrientationLandscapeLeft}

屏幕旋转的两个通知

// 硬件旋转通知,此时界面还未完成旋转可设置界面的旋转UIDeviceOrientationDidChangeNotification// 界面旋转通知,此时界面已经完成旋转UIApplicationDidChangeStatusBarOrientationNotification

判断当前界面方向

BOOL isPortrait=UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication]statusBarOrientation]);BOOL isLandscape=UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication]statusBarOrientation]);

单个页面是否可以旋转

@property(nonatomic,readonly)BOOL shouldAutorotateNS_AVAILABLE_IOS(6_0)__TVOS_PROHIBITED;@property(nonatomic,readonly)UIInterfaceOrientationMask supportedInterfaceOrientationsNS_AVAILABLE_IOS(6_0)__TVOS_PROHIBITED;

面向Orientation的信息:随着屏幕的方向变化

[UISCreen bounds]

[UISCreen applicationFrame]

Status bar frame notifications

Keyboard frame notifications

屏幕旋转时使得View有不同布局的实现方法

// way1-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{[superviewWillTransitionToSize:size withTransitionCoordinator:coordinator];[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>_Nonnull context){if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)){_testTextField.frame=CGRectMake(50,50,150,50);_testButton.frame=CGRectMake(50,120,150,50);}else{_testTextField.frame=CGRectMake(50,50,150,50);_testButton.frame=CGRectMake(220,50,150,50);}}completion:^(id<UIViewControllerTransitionCoordinatorContext>_Nonnull context){}];}

// way2-(void)viewDidLayoutSubviews{[superviewDidLayoutSubviews];if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)){_testTextField.frame=CGRectMake(50,50,150,50);_testButton.frame=CGRectMake(50,120,150,50);}else{_testTextField.frame=CGRectMake(50,50,150,50);_testButton.frame=CGRectMake(220,50,150,50);}}

旋转屏幕方向

NSNumber*value0=[NSNumber numberWithInteger:UIDeviceOrientationUnknown];[[UIDevice currentDevice]setValue:value0 forKey:@"orientation"];NSNumber*value=[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft];[[UIDevice currentDevice]setValue:value forKey:@"orientation"];

需求:APP总体不跟随设备的旋转来改变界面的方向,个别页面横屏展示

实现思路:首先APP应该支持横屏和竖屏的展示,也就是说在TARGETS中的Device Orientation选项勾选横屏和竖屏选项。假设ViewController是根视图,那么当ViewController的- (BOOL)shouldAutorotate方法返回为YES的时候,界面的方向跟随设备方向变化。在NV结构(NavigationController上放置ViewController的结构)中需横屏展示的ViewController可不是根视图,此时界面是否跟随设备旋转取决于根视图NavigationController的- (BOOL)shouldAutorotate方法的返回值。在这种情况中,可重写NavigationController的- (BOOL)shouldAutorotate方法返回Navigation栈中的顶部ViewController的shouldAutorotate,以此达到在单个页面随时控制其横屏的效果。横屏通过Key-Value的方法修改设备方向来达到目的。

一、勾选横屏和竖屏选项

勾选横屏和竖屏选项

二、重写- (BOOL)shouldAutorotate返回栈顶控制器的shouldAutorotate

@interfaceMyNav:UINavigationController@end@implementationMyNav-(BOOL)shouldAutorotate{returnself.topViewController.shouldAutorotate;}@end

三、在将要横屏显示的ViewController中,重写- (BOOL)shouldAutorotate,重点是用全局变量控制它的返回值

@interfaceLandscapeViewController(){BOOL _allowAutorotate;}-(BOOL)shouldAutorotate{[supershouldAutorotate];return_allowAutorotate;}

四、通过Key-Value改变设备方向

_allowAutorotate=YES;NSNumber*value0=[NSNumber numberWithInteger:UIDeviceOrientationUnknown];[[UIDevice currentDevice]setValue:value0 forKey:@"orientation"];NSNumber*value=[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft];[[UIDevice currentDevice]setValue:value forKey:@"orientation"];_allowAutorotate=NO;

转载链接:https://www.jianshu.com/p/3db323293428

相关文章

  • 【转】iOS开发:屏幕的旋转

    Device Orientation:设备方向 typedefNS_ENUM(NSInteger,UIDevice...

  • iOS Rotation

    iOS屏幕旋转学习笔记iOS开发中使用屏幕旋转功能的相关方法 1、基本知识点解读 了解屏幕旋转首先需要区分两种 o...

  • 屏幕旋转

    屏幕旋转 推荐文档 了解UIWindow——UIWindow实践 iOS屏幕旋转问题总结 IOS:屏幕旋转与变换 ...

  • iOS开发:屏幕的旋转

    Device Orientation:设备方向 UIInterfaceOrientation:界面方向 屏幕旋转的...

  • iOS-屏幕旋转截屏相关

    本篇收录各种屏幕旋转知识点等. 1.详解iOS开发中处理屏幕旋转的几种方法2.iOS 个别页面强制横屏,其他页面竖...

  • iOS屏幕方向控制

    iOS屏幕旋转学习笔记 - CocoaChina 苹果开发中文站 - 最热的iPhone开发社区 最热的苹果开发...

  • iOS开发之屏幕旋转

    开发了很久一直没有用到过屏幕旋转,一般app设置固定竖屏也就可以了,但是涉及到视频播放的时候全屏播放就是不得不实现...

  • iOS开发 屏幕旋转viewWillTransitionToSi

    屏幕旋转前后方向的获取

  • Runtime解决屏幕旋转问题

    前言 大家或许在iOS程序开发中经常遇到屏幕旋转问题,比如说希望指定的页面进行不同的屏幕旋转,但由于系统提供的方法...

  • 关于IOS屏幕旋转的问题

    IOS 开发中,屏幕旋转是比较蛋疼的。网络上有很多的关于屏幕旋转的文章,都比较碎片,而且有些做法还很暴力,并且在应...

网友评论

      本文标题:【转】iOS开发:屏幕的旋转

      本文链接:https://www.haomeiwen.com/subject/jvypoctx.html