美文网首页
屏幕旋转

屏幕旋转

作者: DVWang | 来源:发表于2016-08-05 16:40 被阅读0次

在做工程的时候碰到了屏幕旋转的问题,如今已经解决.为大家分享一下

屏幕旋转机制流程

(1)加速计检测到方向变化,发出 UIDeviceOrientationDidChangeNotification 通知。

(2)程序接收到通知,通过 AppDelegate 知会当前程序的 Window。

(3)Window 通知 RootViewController,根据以下设置决定是否旋转。

• Info.plist 中 Supported interface orientations 是否支持该方向

• AppDelegate 中 supportedInterfaceOrientationsForWindow 中是否支持该方向

• RootViewController 中 shouldAutorotate 是否为 YES

• RootViewController 中 supportedInterfaceOrientations 是否支持该方向

(4)RootViewController 通知其 ChildrenViewController,同步旋转操作。

一般情况下 ChildrenViewController 不单独设置,与 RootViewController 保持一致。如果特殊场景需要单独设置,可以通过在 RootViewController 中下放权限,如:NavigationController 可以通过 self.topViewController 下放权限;TabBarController 可以通过 self.selectedViewController 下放权限。但是要注意,即使下放了权限,ChildrenViewController 还是必须遵守 Info.plist 和 AppDelegate 中的设置。

(5)如果存在弹出的 ModalViewController,则不受限于步骤4中的 RootViewController,只根据 Info.plist、AppDelegate 及其自身所支持的旋转设置决定是否旋转。如果 ModalViewController 是经过包装的另一个 RootViewController,则与步骤4原理类似。

在开发的过程中有时我们会碰到项目中所有页面只支持竖屏或者竖屏,还有某一界面为横屏的情况(如:视频播放等)

当根控制器为UINavigationController时:

1>子类化UINavigationController

重写父类方法

- (BOOL)shouldAutorotate

{

return self.topViewController.shouldAutorotate;

}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations

{

return self.topViewController.supportedInterfaceOrientations;

}

设置NavigationController为Window的RootController

在某单独界面不支持旋转时

//是否自动旋转

-(BOOL)shouldAutorotate

{

return NO;

}

//支持返回的方向

-(UIInterfaceOrientationMask)supportedInterfaceOrientations

{

return UIInterfaceOrientationMaskPortrait;

}

//返回优先方向

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

{

return UIInterfaceOrientationPortrait;

}

单独界面支持旋转

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations

{

return UIInterfaceOrientationMaskAllButUpsideDown;

}

-(BOOL)shouldAutorotate

{

return YES;

}

2>所有界面不支持旋转

重写application方法

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

{

return UIInterfaceOrientationMaskPortrait;

}

修改Info.Plist文件Supported interface orientations属性屏幕旋转时触发响应-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator{  [coordinator animateAlongsideTransition:^(id_Nonnull context) {    } completion:^(id_Nonnull context) {

}];

[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

}

有的时候我们会进行强制转屏,在这里为大家介绍一种方法

自定义方法

- (void)interfaceOrientation:(UIInterfaceOrientation)orientation

{

self.currentOrientation = 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]];

int val = orientation;

[invocation setArgument:&val atIndex:2];

[invocation invoke];

}

}

在进行转屏(竖屏)的时候调用

[self interfaceOrientation:UIInterfaceOrientationPortrait];

相关文章

  • iOS 屏幕旋转

    屏幕旋转 认知 期望达到的目的 如何让App支持屏幕旋转 如何让App屏幕旋转 如何保证屏幕旋转后布局不会乱 总结...

  • 屏幕旋转

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

  • 屏幕旋转

    UIDevice.current.setValue(UIInterfaceOrientation.landscap...

  • 屏幕旋转

    import "AppDelegate.h" import "ViewController.h" @interfa...

  • 屏幕旋转

    在做工程的时候碰到了屏幕旋转的问题,如今已经解决.为大家分享一下 屏幕旋转机制流程 (1)加速计检测到方向变化,发...

  • 屏幕旋转

    每个视图控制器都控制着自己的旋转方向,如果需要新的旋转权限需要模态出新的视图控制器(如navigation tab...

  • 屏幕旋转

    当activity设置默认属性的时候:竖屏和横屏旋转可以通过监听onConfigurationChanged来判断...

  • 屏幕旋转

    在创建的vc中 //指定能够支持的orientation有哪些 -(UIInterfaceOrientationM...

  • 屏幕旋转

    本文涉及到的转屏是咱们的app的某个页面设置横竖屏的切换 必须先在appdelegate中实现下面的方法-(UII...

  • 屏幕旋转

    第一步 在 AppDelegate.h 里增加一个属性 用来区分哪个界面可以横屏 哪个界面不可以 第二步 在 Ap...

网友评论

      本文标题:屏幕旋转

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