iOS 横屏/竖屏切换总结
2017-11-13 本文已影响80人
酒深巷子Ya
最近的项目有用到横屏和竖屏的知识点,简单的梳理了一下:
第二步:在appdelegate里面进行一些设置
设置全局属性:
@property (nonatomic, assign) BOOL isRotation;
代理方法
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
if (self.isRotation) {
return UIInterfaceOrientationMaskAll;//根据重力感应
}else {
return UIInterfaceOrientationMaskPortrait;//竖屏
}
}
第三步:在VC或者View里面 模态推出
添加监听事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen:) name:UIDeviceOrientationDidChangeNotification object:nil];//进入全屏
事件
- (void)begainFullScreen:(NSNotification *)noti {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.isRotation = YES;
}
页面消失 或者销毁时
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.isRotation = NO;
}
到此横屏竖屏 即全部OK了
其它可能用到的方法:
进入一个界面强制横屏或竖屏
- (void) setScreenPortrait {
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 = UIInterfaceOrientationPortrait;//竖屏 也可设置竖屏
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
总结:1.在进行横屏竖屏切换的时候appdelegate里面的方法会首先通知方法触发
2.所有涉及横屏的界面尽量模态推出,模态已经可以满足所有横屏/竖屏的需求,这样处理起来也要简单很多,push出来的界面还要对你navigationbar和父类等进行处理,很是麻烦,网上也有很多类似的文章,不做讲解。
持续更新~ing